简体   繁体   English

Javascript正则表达式电话验证

[英]Javascript regex phone validation

Can anyone suggest a js function to validate a phone number which has to start with 0 and be followed by any other digit? 谁能建议一个js函数来验证必须以0开头且后跟任何其他数字的电话号码? I am not concerned about the size, because I have maxlength="10" in my html code. 我不关心大小,因为我的html代码中有maxlength =“ 10”。 I now use a simple js function in order to restrict letters and other symbols from the phone nr. 我现在使用一个简单的js函数来限制电话nr中的字母和其他符号。 Any help is welcome! 欢迎任何帮助!

You want to use the .match function of string variables. 您要使用字符串变量的.match函数。 eg. 例如。

var myString = $("#myPhoneNumebrField").val();
if (myString.match(/0[0-9]+/))
{
  //Valid stuff here
}
else
{
  //Invalid stuff here
}

As suggested by others, you probably want a minimum size in which case you can change the regex to be /0[0-9]{8,10}/ which will make the regex only match if the string is between 8 and 10 characters long (Inclusive). 正如其他人所建议的那样,您可能需要最小大小,在这种情况下,可以将正则表达式更改为/0[0-9]{8,10}/ ,这将使正则表达式仅在字符串介于8和10个字符之间时才匹配长(含)。

This regex should do: /^0[0-9]+$/ 此正则表达式应执行以下操作: /^0[0-9]+$/

But you should also implement a minimum size . 但是您还应该实现最小大小

  1. You do not need a regex for this, instead write a function which iterates over the string. 为此,您不需要正则表达式,而是编写一个对字符串进行迭代的函数。
  2. Try to do it yourself, you can start with Learning Regular Expressions . 尝试自己做,您可以从学习正则表达式开始。 You will learn much more if you do not quote a ready made solution from SOF. 如果您不引用SOF的现成解决方案,您将学到更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM