Hello!
I have a text field where a user can enter either a(n):
- American zip code (example: "02475")
- Canadian zip code (example: "A3F 4G6")
- A "city/state abbreviation" combination (example: "Toronto, ON", or "Las Vegas, NV")
For the most part, users don't have much of a problem with the first two criteria, but some people have some trouble with the third, either by entering simply a state ("NB", or "Texas"), a city, or spelling out the state name "Chicago, Illinois". I need to put together some validation that makes sure that:
- If the entered text is not an american or canadian zip code (IOW, if the entered text contains no numbers at all - in that case, move on to the next validation check)
- Check to see if the third to last character is non alphanumeric (if they entered the city/state combination correctly, there would be some sort of non-alphanumeric separator between the city and state abbreviation)
- if the entered text doesn't fit the criteria, return the error.
This is what I think I've got for code, but I"m sure the syntax is off. Is there a chance someone could please point me in the right direction? Thanks!
function hasNumbers(string) {
var regex = /\d/g;
return regex.test(string);
}
function validateOriginFormat() {
var error = "";
if (
(document.form.Search_Type_1.checked == true)&&
(!hasNumbers(document.form.Search_Radius_Point.value))&&//if the value of the field has no numbers in it, and, thusly is not an american or canadian zip code
(document.form.Search_Radius_Point.value.substr(-3, 1)//and the third to last character of the value is not a letter or number
)
{
error = "Please use a valid city/state format (example: Boston, MA or Toronto, ON) in Step 3!\n"//recognize the the intended input was a city and state, but the proper format of "City, ST" was not used.
}
return error;
}