简体   繁体   English

Javascript Regex:验证电话号码

[英]Javascript Regex: Validate phone number

I want to validate a 11 digit phone number that must start with 09 (ie 09123456789) using javascript for client side check.我想验证一个必须以 09(即 09123456789)开头的 11 位电话号码,使用 javascript 进行客户端检查。

this is my code:这是我的代码:

function validatePhone(number) {
    var re = /^09[0-9]{9}$/;
    return re.test(number);
}

It worked correctly as far as I know at first but now it return invalid for any number I use.据我所知,它起初工作正常,但现在对于我使用的任何数字都返回无效。 Does anyone know what is wrong with my code?有谁知道我的代码有什么问题? By the way as I'm using jQuery, if there is a better way with jquery that would be much better.顺便说一句,当我使用 jQuery 时,如果有更好的 jquery 方法会更好。

I am assuming you are sending an integer in the argument, pass it as a string我假设您在参数中发送一个整数,将其作为字符串传递

function validatePhone(number) {
    var re = /^09[0-9]{9}$/;
    return re.test(number);
}

​alert(validatePhone('09123456789'));​​​
// returns true    
alert(validatePhone(09123456789));    
// returns false    ​

http://jsfiddle.net/3bFwd/ http://jsfiddle.net/3bFwd/

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

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