'how to validate 10 digit phone number?

I need to 10 digit number validation in JS code. I've put the "Contact already exists" validation code from the databse. But I'm not recognize the 10 digit validation code. my js code:

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 ?



Solution 1:[1]

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;
  }
}

Solution 2:[2]

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

Solution 3:[3]

Just use this to validate whether the string is only number or not

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

Solution 4:[4]

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>

Solution 5:[5]

You don't need to validate 10 digits, just bind with input type as Number. It allow only numbers as an input. As per your code when its length will be 10 so automatically it will call 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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rehan Shikkalgar
Solution 2 Community
Solution 3 Mahbub Alam
Solution 4 Aman Kumar
Solution 5 Sunoo