简体   繁体   中英

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> 

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