I’m fairly new to Javascript and need a little help. How do I validate a field to ensure it contains at least one alpha character and at least one numeric character? Any help is appreciated.
Regular expressions have a test() function, which returns true or false depending on whether a given match was found in a given string. For instance, here is a simple regex:
var my_regex = /[a-z]/;
That expression will match any lower case letter in a string. So, you could do this:
var my_regex = /[a-z]/;
var result = my_regex.test("123456a");
That bit on the right of the equals sign says to search the string "123456a" for the pattern stored in the variable my_regex. Since the symbols stored in the variable my_regex will match any lower case letter, if any lower case letter is found in the string, then result will be true, otherwise result will be false.