javascript - Regular Expression for Alphabetical text with some exceptions -
i looking regular expression text field accepts alphabets, dots(.), brackets, -, &
a.b.c abc (new), & co. a-abc.
i have regular expression:
/^[a-z.&() ]+$/
code:
var regex = /^[a-z][a-z.()&-]+$/; var sel = document.getelementbyid("textboxid").value; if (sel != "-- select --") { if (!regex.test(sel.tostring())) { alert("please use proper name."); } } return false;
i want string mandatory start alphabet. seems not working when add - same, may syntax wrong.
first, note [a-z]
not same [a-za-z]
, meant. if @ an ascii table, can see range a
z
includes several special characters may not intend include.
second, since dash -
denotes range in character class (as above), must included last (or first, more commonly last) in order interpreted literal dash.
so, want this:
/^[a-za-z.()&-]+$/
and assert starts alphabet:
/^[a-za-z][a-za-z.()&-]*$/ ^^^^^^^^ ^
(notice i've changed +
quantifer *
quantifier.)
finally, @explosionpills had done, can simplify expression making case-insensitive:
/^[a-z][a-z.()&-]*$/i
Comments
Post a Comment