Home > database >  how to not allow user to give special characters, numbers , spaces only in first place of word in ht
how to not allow user to give special characters, numbers , spaces only in first place of word in ht

Time:05-11

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'/>

CodePudding user response:

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>

CodePudding user response:

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.

  • Related