简体   繁体   中英

how to not allow user to give special characters, numbers , spaces only in first place of word in html using regex

How to not allow user to give special characters, numbers, spaces only in first place of word in html using regex.

<label><span>Current Carrier</span></label>
<input name='Current Carrier'  type='text' class='form-control' pattern='[a-zA-Z]+$' for Physical Address'/>

You can use

pattern="[A-Za-z].*"

Details :

  • The pattern will be compiled into a ^(?:[A-Za-z].*)$ regex pattern that matches an ASCII letter at the start of string and then can contain any zero or more chars other than line break chars, as many as possible, till the end of input string
  • The .* is required because the pattern regex must match the whole input

See the live demo below:

 input:valid { color: black; } input:invalid { color: red; }
 <form name="form1"> <input pattern="[A-Za-z].*" title="Please enter the data in correct format." /> <input type="Submit" /> </form>

You can try the carrot symbol in regex -> Something like /^[A-Za-z]/ -> this will match the first character and tells that it must be a letter, it cannot be a space, a number or a special character.

You can write the remaining regex after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM