简体   繁体   English

Javascript 用于电话号码验证的正则表达式。(字符串仅包含至少 1 个数字、0 个或多个空格和 '-'

[英]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.

相关问题 javascript正则表达式电话号码验证 - javascript regular expression phone number validation 正则表达式电话号码验证,使用空格和最小位数的字符 - Regular-Expression Phone number validation with spaces and characters with minimun digits 电话号码验证正则表达式 - Phone number Validation regular expression 正则表达式电话号码验证 - Regular Expression Phone Number Validation 从javascript中的字符串中提取10位手机号码的正则表达式 - Regular expression to extract a 10 digit mobile number from string in javascript 包含一个或多个指定字母的字符串的Javascript正则表达式 - Javascript Regular Expression for a string that contains one or more of specified letters 如何在javascript中测试字符串是否包含带有正则表达式的数字? - How to test if a string contains a number with regular expression in javascript? 使用Java中的正则表达式验证10位美国风格的电话号码 - validate a 10 digit USA style phone number using a Regular Expression in Javascript 正则表达式:允许字母,数字和空格(至少有一个字母不是数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number) 正则表达式:允许字母,数字和空格(至少包含一个字母或数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM