i need to validate address1 , address 2 and city using jquery regex..
Used for a street address. Legal Characters: A-Z, a-z, 0-9, hyphen, slash and single space. Illegal Character: leading space, trailing space, adjacent spaces, and other symbols.
Used for a city. Legal Characters: A-Z, a-z, and single space. Illegal Character: leading space, trailing space, adjacent spaces, and symbols.
sample:
Address1: { required: true, regex: “A-Za-z0-9*” }
I assume you’re doing this for some project, so you should really figure out how to do the regex on your own.
However, I will tell you. It is my understanding that you need to give the regex for each field separately and they will be validated separately.
Start with a letter/number/dash/slash followed by an optional space. Follow with any number of l/n/d/s or l/n/d/s followed by a space (this forces there to be only one space because each space must be preceded by a l/n/d/s). End with l/n/d/s.
Pretty much the same as above, just without worrying about numbers, dashes, and slashes.
Check out unit tests: http://jsfiddle.net/LJCzR/4/
As stated above, though if you are not doing this for academic reasons, then I wouldn’t validate on leading/trailing space or duplicate space at all. I would silently clean it. Additionally, you simply can’t account for every case. This kind of validation is just annoying.