javascript - jQuery alphabetical only with numeric -
i'm having difficulties jquery validation. need make input validation alphabetical numeric, shouldn't validate if put numeric value.
for example: lorem ipsum 123 should valid, lorem ipsum should valid, 123 shouldn't valid.
i use code doesn't work properly.
e.find('.alphanum').keypress(function(e) { var regex = new regexp("^[a-za-z0-9 ]+$"); var str = string.fromcharcode(!e.charcode ? e.which : e.charcode); if (regex.test(str)) { return true; } e.preventdefault(); return false; });
this might useful:
/^(?:[a-za-z0-9 ]*[a-za-z ][0-9]*)+$/ it same thing 1 provided qwertiy, might simpler understand (which, imho, quality in when comes regular expressions...).
what happens form non-capturing group of 0 or more allowed characters, 1 required character, , 0 or more allowed not required characters, , match group. if don't want allow only-whitespace strings either, move space second third character class, i.e.
/^(?:[a-za-z0-9 ]*[a-za-z][0-9 ]*)+$/ you can think of 3 character classes as
- all characters allowed in string.
- all characters must occur @ least once
- all characters appear in 1., not in 2.
it's possible more inefficient qwertiy's answer, solution matches e.g. " ", while second 1 in answer doesn't.
you can fiddle regex, , test results, here: https://regex101.com/r/ib6pe1/3
Comments
Post a Comment