简体   繁体   中英

Regex avoid numeric and special characters and allow space between words

I need validation for Full name text box which should avoid numeric and special characters, but allow space between words. See my JavaScript code here:

function jsPreventNumeric(obj) {
    obj.value = obj.value.replace(/[^a-z]/gmi, '').replace(/\s+/g, '');
}

It's working fine except it's not allowing space between words. I don't have much knowledge to edit that regex. Can anyone help me to adjust this regex to allow space also?

\\s is the escape sequence for whitespace. So your second replace removes all whitepspaces explicitly. /[^az]/ means all characters that are not characters from az. Add \\s so it only removes characters that are not az or whitespace

function jsPreventNumeric(obj){
   obj.value.replace(/[^a-z\s]/gmi,"");
}

I dont see the picture. Try this: ^[\\\\p{L} .'-]+$

解决问题的方法:

obj.value = obj.value.replace(/[^a-z\s]/gmi, "");

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