简体   繁体   English

如何不允许用户使用正则表达式在 html 中仅在单词的第一位给出特殊字符、数字、空格

[英]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.如何不允许用户使用正则表达式在 html 中仅在单词的首位给出特殊字符、数字和空格。

<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该模式将被编译成^(?:[A-Za-z].*)$正则表达式模式,该模式与字符串开头的 ASCII 字母匹配,然后可以包含除换行符以外的任何零个或多个字符,如尽可能多,直到输入字符串的末尾
  • The .* is required because the pattern regex must match the whole input .*是必需的,因为pattern正则表达式必须匹配整个输入

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.您可以尝试正则表达式中的胡萝卜符号 -> 类似于/^[A-Za-z]/ -> 这将匹配第一个字符并告知它必须是字母,不能是空格、数字或特殊字符特点。

You can write the remaining regex after that.您可以在那之后编写剩余的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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