简体   繁体   English

如何验证 10 位电话号码?

[英]how to validate 10 digit phone number?

I need to 10 digit number validation in JS code.我需要在 JS 代码中进行 10 位数字验证。 I've put the "Contact already exists" validation code from the databse.我已经从数据库中添加了“联系人已经存在”的验证码。 But I'm not recognize the 10 digit validation code.但我不认识 10 位验证码。 my js code:我的js代码:

function checkcontact() {
    var contact=document.getElementById( "UserContact" ).value;

    if(contact.length==10) {
        $.ajax({
            type: 'post',
            url: 'index.php=checkemail',
            data: {
                u_contact:contact,
            },
            success: function (response) {
                if(response=="not_exist") {
                    return true;    
                } else {
                    $( '#contact_status' ).html(response);
                }
            }
        });
    }
}

my input type from 

<input class="form-control" type="text" maxlength="10"  id="UserContact" onkeyup="checkcontact(); if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" placeholder="Contact Number Here" required/>
<span id="contact_status"></span> </div>

How to validate 10 digit number in checkcontact() function in js code ?如何在js代码中的checkcontact()函数中验证10位数字?

try this,试试这个,

function checkcontact(){
var contact=document.getElementById( "UserContact" ).value;

var validate = check(contact);
if(validate){
    $.ajax({
        type: 'post',
        url: 'index.php=checkemail',
        data: {
        u_contact:contact,
        },
        success: function (response) {
            if(response=="not_exist"){
                return true;    
            }
            else{
                $( '#contact_status' ).html(response);
            }
        }
    });
}
}

function check(var number){
 var reg = /^[0-9]{1,10}$/;
 var checking = reg.test(number); 

  if(checking){
    return true;
  }else{
    return false;
  }
}

Try this..试试这个..

var val = contact
  if (/^\d{10}$/.test(val)) {
  // value is ok, use it
  } else {
  alert("Invalid number; must be ten digits")
  return false
}

Reference 参考

Just use this to validate whether the string is only number or not只需使用它来验证字符串是否只是数字

 var yourValue = '123456789'; var isNumber = /^\\d+$/.test(yourValue); alert(isNumber);

Check this code检查此代码

 $(function() { $('#UserContact').blur(function() { var contact = $("#UserContact").val(); if(contact.length==10){ alert(contact) //return false; $.ajax({ type: 'post', url: 'index.php=checkemail', data: {u_contact:contact}, success: function (response) { if(response=="not_exist"){ return true; } else { $( '#contact_status').html(response); } } }); } }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <input class="form-control" type="number" maxlength="10" id="UserContact" placeholder="Contact Number Here" required/> <span id="contact_status"></span> </div>

You don't need to validate 10 digits, just bind with input type as Number.您不需要验证 10 位数字,只需将输入类型绑定为 Number。 It allow only numbers as an input.它只允许数字作为输入。 As per your code when its length will be 10 so automatically it will call ajax.根据您的代码,当其长度为 10 时,它会自动调用 ajax。

<input class="form-control" type="number" maxlength="10"  id="UserContact" onkeyup="checkcontact(); if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" placeholder="Contact Number Here" required/>
      <span id="contact_status"></span>

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

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