1

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 ?

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20

5 Answers5

-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;
  }
}
Rehan Shikkalgar
  • 1,029
  • 8
  • 16
-1

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

Community
  • 1
  • 1
Minksmnm
  • 254
  • 3
  • 8
-1

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

var yourValue = '123456789';
var isNumber = /^\d+$/.test(yourValue);
alert(isNumber);
Mahbub Alam
  • 368
  • 2
  • 6
-1

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>
Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
-1

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>
Sunoo
  • 1
  • 6