html - Not allow space as a first character and allow only letters using jquery -


im using jquery name validation have tried code given below

$("#contactname").keypress(function(e) {     if(e.which < 97 /* */ || e.which > 122 && (e.which < 65 || e.which > 90)) {        e.preventdefault();     }          }); 

the above code working fine allow letters , not allow numbers not allow space.so want should allow letters (both small , capital letters) should not allow numbers , special characters , accept space except first character , please tell me how restrict when copy paste.

can use html5 attribute pattern? see mdn article on more information.

using regex of ^[a-za-z][\sa-za-z]* seems cover requirements.

so like:

<div>username:</div>  <input type="text" pattern="^[a-za-z][\sa-za-z]*" title="can use upper , lower letters, , spaces must not start space" />


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -