如何在javascript中验证手机和手机号码 [英] how to validate phone and mobile number in javascript

查看:93
本文介绍了如何在javascript中验证手机和手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript中验证手机和手机号码。

例如手机号码-044 27221343.手机号码9254034466/8428664422

/ 7666544221

解决方案

Javascript还支持RegExp(正则表达式)。我建议使用Expresso的电话号码regexp示例作为基础。他们考虑了许多事情,比如括号中的区号,空格或数字组之间的破折号等。你需要考虑你想要支持的内容:

你的本地约定/数字规则

国内区号

国际号码和拨号前缀

等等。



这并不像有些人想的那么简单。



彼得



这里很简单使用Javascript正则表达式的示例

  var  pattern = / ^ \(\d { 3 } \)\ * * \d { 3 }(?: -  | \ s * )\d { 4 } 


/
if (pattern.test(string)){
// 字符串看起来像一个好的(美国)电话带有可选区号的数字,sp中间的ace或短划线
}

[/ edit]


  function  Validate()
{
var x = document .form1.txtPhone.value;
var y = document .form1.txtMobile.value;
if isNaN (x)|| x.indexOf( )!= - 1)
{
alert( 输入数值
return ;
}
if (x.length> 8)
{
alert( 输入8个字符);
return false ;
}
if (x.charAt( 0 )!= 2
{
alert( 它应以2)开头;
返回 false
}

if isNaN (y)|| y.indexOf( )!= - 1)
{
alert( < span class =code-string>输入数值

return false ;
}
if (y.length> 10)
{
alert( 输入10个字符);
return false ;
}
if (y.charAt( 0 )!= 9
{
alert( 它应以9)开头;
返回 false
}
}


how to validate phone and mobile number in javascript.
for example phone no -044 27221343. mobile no-9944034466/8428664422
/7666544221

解决方案

Javascript also supports RegExp (regular expressions). I would suggest using Expresso's phone number regexp examples as a base. They have thought about lots of things like area codes in brackets, spaces or dashes between groups of digits, etc. You will need to think about what you want to support:
your local conventions / rules for numbers
domestic area codes
international numbers and dialling prefix
and so on.

It's not as simple as some people think.

Peter

[edit] Here's a simple example of using a Javascript regexp

var pattern = /^\(\d{3}\)\s*\d{3}(?:-|\s*)\d{4}


/ if (pattern.test(string)) { // string looks like a good (US) phone number with optional area code, space or dash in the middle }

[/edit]


function Validate()
        {
            var x = document.form1.txtPhone.value;
            var y = document.form1.txtMobile.value;
           if(isNaN(x)||x.indexOf(" ")!=-1)
           {
              alert("Enter numeric value")
              return false; 
           }
           if (x.length>8)
           {
                alert("enter 8 characters");
                return false;
           }
           if (x.charAt(0)!="2")
           {
                alert("it should start with 2 ");
                return false
           }

           if(isNaN(y)||y.indexOf(" ")!=-1)
           {
              alert("Enter numeric value")
              return false; 
           }
           if (y.length>10)
           {
                alert("enter 10 characters");
                return false;
           }
           if (y.charAt(0)!="9")
           {
                alert("it should start with 9 ");
                return false
           }
        }


这篇关于如何在javascript中验证手机和手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆