开发者

Checking input values for special symbols

开发者 https://www.devze.com 2023-01-18 10:09 出处:网络
I am having two text fields in my form. The data to be entered in the field are Name and City respectively开发者_Python百科. I want to check that the user has not entered any special symbols like !,@,

I am having two text fields in my form. The data to be entered in the field are Name and City respectively开发者_Python百科. I want to check that the user has not entered any special symbols like !,@,#........ i.e, the only thing user should enter must be belonging to a-z,A-Z, though the user can enter Underscore(_), but no numbers, no special symbols.

I want to check this using JavaScript, how can this be achieved.

Thanks in advance.


A classic problem that's usually solved with the help of regular expressions.

var myString = "London";
if (myString.match(/^[a-zA-Z_]+$/)) {
    // Success
}

If you want to allow spaces, like for New York, change the pattern to /^[a-zA-Z_\s]+$/.

0

精彩评论

暂无评论...
验证码 换一张
取 消