[英]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 hyphen 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.您可以使用 Trever 引用的标准正则表达式,但您也可以简单地在字符串上运行另一个
.match(/\d+/g)
,如果两者都成功,您可以确保它符合您的要求并且还具有至少一位数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.