简体   繁体   English

正则表达式可验证空白字段,仅允许包含字符的空格

[英]Regular Expression to validate empty fields allowing space with characters only

I have a form that use a regular expression that validate characters only alongwith whitespace, but I have a problem, this doesn't validate empty fields. 我有一个使用正则表达式的表单,该正则表达式仅与空格一起验证字符,但是我有一个问题,这不能验证空字段。 My expression regular is this : 我的常规表达是这样的:

regex = /^\s*[a-zA-Z ]{2,30}$/;

If you want to validate an empty field as well, you can provide an alternative for the whole pattern by putting |^$ onto the end, thus matching the empty string too (that is, match the position at the beginning of the string, immediately followed by the position at the end of the string): 如果还想验证一个空字段,则可以通过在整个字符串的末尾添加|^$ ,从而为整个模式提供替代方法,从而也匹配空字符串(即,立即匹配字符串开头的位置)然后是字符串末尾的位置):

 <form> <input pattern="^\\s*[a-zA-Z ]{2,30}$|^$"> <button>submit</button> </form> 

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

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