I have a string which is of format ABC1234567
var value = "ABC1234567"
var first3Letters = value.substring(0,3); // ABC
var next7Letters = value.substring(3,7); //1234567`
Now I want to validate whether variable first3Letters contains only alphabets, and variable next7letters contains only integers.
How do I do this?
Here’s one way:
See it in action at jsFiddle.
Regex explained:
^specifies the beginning of the string.&specifies the end of the string.[a-z]is any word character (A, B, C, etc., but NOT numbers or the the underscore — which\wwould allow).\dis any number character (0, 1, 2, etc.)+means one or more of the previous.iat the end (i.test) tells JS to run a case-insensitive search.So the regexes are essentially saying, “From the beginning to the end, are there nothing but 1 or more (word or number) characters.”
See also: Regular expressions tutorial.