Javascript regular expression for phone number validation.(String contains only at least 1 digit, 0 or more spaces and '-'
Currently I have an expression:
numMatches = phone.match(/[^\d\s\-]/gi);
if (numMatches != null) {
alert(invPhnNo);
}
This gives an alert if any characters other than digit, space and hyphen are entered. But still accepts if only hyphe开发者_如何学编程n and spaces are given without a single digit. Now i would want it to alert if atleast a digit is not there. So a digit is mandatory. Can have zero or more spaces and hyphen and no other characters.
Can anyone suggest an approach to this?
You could either use the standard regexp refered to by Trever, but you could also simply run another .match(/\d+/g)
on the string and if both succeed, you can be sure that it complies with your requirements and also has at least one digit.
精彩评论