简体   繁体   English

电话号码格式检查 javascript

[英]phone number format checking javascript

I have made a website with JS and php and as you can see below is my code at paste bin, I have looked around and seen regular expressions but I don't know how to use them or what I should use exactly....But basically I want to just allow the user to enter north american phone numbers and if they enter anything else then give them a message to enter a valid phone number...我用 JS 和 php 创建了一个网站,正如您在下面看到的那样,我在粘贴箱中看到了我的代码,我环顾四周并看到了正则表达式,但我不知道如何使用它们或我应该确切地使用什么......但基本上我只想允许用户输入北美电话号码,如果他们输入其他任何内容,然后给他们一条消息以输入有效的电话号码......

Below is my code to help you understand what I have..What I need and what my site's interface looks like...下面是我的代码,可帮助您了解我所拥有的......我需要什么以及我的网站界面是什么样的......

http://pastebin.com/8NwURm0G http://pastebin.com/8NwURm0G

possible phone numbers user can enter are 9058554678 OR 4167641689...用户可以输入的可能电话号码是 9058554678 或 4167641689...

This is how the the list looks:这是列表的外观:

http://dev.icalapp.rogersdigitalmedia.com.rogers-test.com/Edit.php http://dev.icalapp.rogersdigitalmedia.com.rogers-test.com/Edit.php

Here is regex that I have laying around to only match 10 digit NA numbers, based off the NANP这是我放置的正则表达式,仅匹配 10 位 NA 数字,基于NANP

/(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})/
^\d{10}$

That matches 10-digit numbers.匹配 10 位数字。

^1?\d{10}$

That matches 10-digit numbers with an optional 1 at the front.这匹配 10 位数字和前面的可选 1。

^(?:1-?\s*)?(?:\(?:\d{3}\)|\d{3})\s*-?\d{4}$

That matches digit groupings like 1-444-555-6666 or (444) 555-6666 , and some related ones.这匹配数字分组,如1-444-555-6666(444) 555-6666以及一些相关的。

A quick search on google yielded the following result.谷歌上的快速搜索产生了以下结果。 You might want to combine it with mitchellhislop's answer.您可能想将它与 mitchellhislop 的答案结合起来。

function isPhoneNumber(s)
{

 // Check for correct phone number
 rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);

 if (!rePhoneNumber.test(s)) {
      alert("Phone Number Must Be Entered As: (555) 555-1234");
      return false;
 }

return true;
}

It's worth reading up on regular expressions before using them, since they can be a little tricky and unintuitive.在使用正则表达式之前值得阅读它们,因为它们可能有点棘手和不直观。

Good tutorial: http://www.regular-expressions.info/quickstart.html好教程: http://www.regular-expressions.info/quickstart.html

Based on what you list as a valid input, you're looking to see if the user entered in 10 digits.根据您列为有效输入的内容,您正在查看用户是否输入了 10 位数字。 This can be done by checking if a character is a digit, and then seeing if there are 10 of them, which would be the regular expression: [0-9]{10}.这可以通过检查字符是否为数字来完成,然后查看它们是否有 10 个,这将是正则表达式:[0-9]{10}。 Example on how to use:使用方法示例:

var myRegEx = /[0-9]{10}/;
var itemsToTest = ["9058554678","abc"];

for (var ii=0;ii<itemsToTest.length;ii++) {
    if ( myRegEx.test(itemsToTest[ii]) ) {
        alert(itemsToTest[ii] + " is valid!");
    }
}

Test: http://jsfiddle.net/87ea8/测试: http://jsfiddle.net/87ea8/

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

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