In javascript i am attempting to validate a first name in a form using
var regex = /([a-zA-Z'-])+/;
however if there is a character is accepts such as a letter and a character it shouldnt accept such as a number it still validates it. It only doesn't validate it if it is all numbers. Only letters, hypens and apostrophes are allowed in firstName. Am i supposed to add something to the regex code to make it validate everything in the text box?
I suggest you to add anchors. Anchored regex will do an exact string match.
var regex = /^[a-zA-Z'-]+$/;
^
asserts that we are at the start and $
asserts that we are at the end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.