开发者

phone number validation in javascript

开发者 https://www.devze.com 2023-02-28 11:26 出处:网络
I need aphone validation code that accept only digits and dashes like this format 777-777-7777 , length is equal to 12. I\'m new to Jquery and I really appreciate your help.

I need a phone validation code that accept only digits and dashes like this format 777-777-7777 , length is equal to 12. I'm new to Jquery and I really appreciate your help.

here my code:

var x=document.forms["myForm"]["phone"].value
if (x==null || x=="")
  {
  alert("Business phone number must be filled out");
      return false;
  }
开发者_如何学Go

Thanks!


You can use a simple RegExp to test 3digits-dash-3digits-dash-4digits as follows:

var phoneTest = new RegExp("\\d{3}-\\d{3}-\\d{4}"),
    phoneNum = "777-777-7777",
    otherNum = "66-666-6666";

phoneTest.test(phoneNum); // returns true
phoneTest.test(otherNum); // returns false
0

精彩评论

暂无评论...
验证码 换一张
取 消