正则表达式不起作用?

时间:2016-08-28 05:21:07

标签: javascript html regex

让我们直接跳进去:

JS

function detailCheck() {
  var phNoLength = document.getElementById('phNo').value.length;  //get value for phone number from form for checking
  var cardNoLength = document.getElementById('cardNo').value.length; //get value for card number length for checking 
  var postCodeLength = document.getElementById("postCode").value.length //get value for post code length
  var a = /^[-+]?[0-9]+$/; 
  var b = /^[A-Za-z]+$/; 
//if form is not got all inputs filled 
 for (var i = 0; i < 5; i++) {
   details = document.getElementById("myForm")[i].value;
   if (details === "") {
     var i = ("Please enter ALL your details."); 
     document.getElementById("formTital").innerHTML=i;
    return;
    }
}
//check if names have numbers in them
if(b.test(document.getElementById("fName").value)){                                                                     
 if(b.test(document.getElementById("lName").value)){
 //if names too short
  if(fName < 3) {i = "Please enter a longer first name."; document.getElementById("formTital").innerHTML=i; return;}
   if(lName < 3) {i = "Please enter a longer last name."; document.getElementById("formTital").innerHTML=i; return;}
 //if phone number not number
    if(a.test(document.getElementById("phNo").value)){
 //if phone number too short 	
     if(phNoLength != 7) {var i ="Please use a phone number with 7 digits";document.getElementById("formTital").innerHTML=i; return;}
 //if CC is not a number
      if(a.test(document.getElementById("cardNo").value)){
 //if CC is too short
       if(cardNoLength != 4) {var i ="Please use a card number with 4 digits";document.getElementById("formTital").innerHTML=i; return;}
 //if post code is not a number
        if(a.test(document.getElementById("postCode").value)){
         if(postCodeLength != 4) {var i ="Please use a card number with 4 digits";document.getElementById("formTital").innerHTML=i; return;}


     } else {var i ="Please only use numbers in your post code."; document.getElementById("formTital").innerHTML=i; return;}

    } else {var i ="Please only use numbers in your credit card."; document.getElementById("formTital").innerHTML=i; return;}

   } else {var i ="Please only use numbers in your Phone number."; document.getElementById("formTital").innerHTML=i; return;}
   
  } else {var i = "Please don't use numbers in your last name."; document.getElementById("formTital").innerHTML=i; return;}
  
 } else {var i = "Please don't use numbers in your first name."; document.getElementById("formTital").innerHTML=i; return;}
toggleContent();
}
 <form id="myForm" action="form_action.asp">
    First name: <br> <input class="formInput" type="text" id="fName" name="fName"><br>
    Last name: <br> <input class="formInput" type="text" id="lName" name="lName"><br>
    Phone Number: <br> <input class="formInput" type="number" id="phNo" name="phNo" maxlength="7"><br>
    Credit Card Number: <br> <input class="formInput" type="password" id="cardNo" name="cardNo" maxlength="4"><br>
    Address: <br> <input class="formInput" type="text" id="address" name="address"><br>
    Post code: <br> <input class="formInput" type="number" id="postCode" name="postCode" maxlength="4"><br>
   </form>

我可以将输入传递到电话号码(如-234567)并尽管var a = /^[-+]?[0-9]+$/;通过。如果我添加123456+,例如,没有任何反应。

2 个答案:

答案 0 :(得分:0)

试试这段代码:

var a = /^[-+]?[0-9]+\+$/;

&#13;
&#13;
var a = /^[-+]?[0-9]+\+$/;
console.log("123456+".match(a))
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你的问题并不清楚你真正想要的是什么。假设您想要将美国电话号码与RegExp匹配,以下内容将代表美国10或7位数的电话号码:

var phoneNumberRegExp = /^(?:\(?([2-9]\d{2})[)-]? ?)?([2-9]\d{2})-?(\d{4})$/;

这应该匹配:

2134567
213-4567
213-456-7890
(213)456-7890
(213) 456-7890

注意:01不是区号或前缀的有效第一个数字(7位电话号码的前3个数字)。

第一个capture group将是区号。第二个是前缀(交换)。第三是用户号码。

但是,正则表达式实际上不足以验证电话号码。您最好使用像Google's libphonenumber这样的专业库。