Validating input values ​​for special characters

I have two text fields in my form. The data that you must enter in the field is the name and city, respectively. I want to check that the user has not entered any special characters, such as !, @, # ........ that is, the only thing the user must enter must be az, AZ, although the user can enter Underscore (_ ), but not numbers, not special characters.

I want to test this with JavaScript, how can this be done.

Thanks in advance.

+3
source share
1 answer

A classic problem usually solved with regular expressions.

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

, , -, /^[a-zA-Z_\s]+$/.

+4

Source: https://habr.com/ru/post/1767834/


All Articles