9

I want to validate cell number using JavaScript.

Here is my code.

if(number.value == "") {
    window.alert("Error: Cell number must not be null.");
    number.focus();
    return false;
}

if(number.length != 10) {
    window.alert("Phone number must be 10 digits.");
    number.focus();
    return false;
}

Here is the issue, when I submit the form with out entering the phone number, it is showing the error cell number must not be null. it works fine.

When I submit the form with cell number less than 10 digits, it is showing phone number must be 10 digits. It is also fine.

The problem is when I submit the form with 10 digits, then also it is showing the error phone number must be 10 digits.

Please help me. Thank You.

And also need the validation code for only digits for cell number.

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
sapan
  • 259
  • 3
  • 6
  • 16

9 Answers9

25

If number is your form element, then its length will be undefined since elements don't have length. You want

if (number.value.length != 10) { ... }

An easier way to do all the validation at once, though, would be with a regex:

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

\d means "digit," and {10} means "ten times." The ^ and $ anchor it to the start and end, so something like asdf1234567890asdf does not match.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • It worked to some extent but when i enter more than 10 digits. It is not showing error message. why? and also if i enter 10 digits along with some more additional characters, it is not showing error message. – sapan Sep 23 '13 at 02:33
  • It worked along with the answer given by user : sushain97 Thank You both. – sapan Sep 23 '13 at 02:41
  • @sapan Sorry, I fixed it. It should show the error message with more than ten digits now. – tckmn Sep 23 '13 at 12:02
  • Thank you once again for your help. – sapan Sep 23 '13 at 13:43
6
function IsMobileNumber(txtMobId) {
    var mob = /^[1-9]{1}[0-9]{9}$/;
    var txtMobile = document.getElementById(txtMobId);
    if (mob.test(txtMobile.value) == false) {
        alert("Please enter valid mobile number.");
        txtMobile.focus();
        return false;
    }
    return true;
}

Calling Validation Mobile Number Function HTML Code -

Rahul Dadhich
  • 1,213
  • 19
  • 32
Shankar M
  • 61
  • 1
  • 1
3

function isNumber(evt) {
  evt = (evt) ? evt : window.event;
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    alert("Please enter only Numbers.");
    return false;
  }

  return true;
}

function ValidateNo() {
  var phoneNo = document.getElementById('txtPhoneNo');

  if (phoneNo.value == "" || phoneNo.value == null) {
    alert("Please enter your Mobile No.");
    return false;
  }
  if (phoneNo.value.length < 10 || phoneNo.value.length > 10) {
    alert("Mobile No. is not valid, Please Enter 10 Digit Mobile No.");
    return false;
  }

  alert("Success ");
  return true;
}
<input id="txtPhoneNo" type="text" onkeypress="return isNumber(event)" />
<input type="button" value="Submit" onclick="ValidateNo();">
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Barath Kumar
  • 439
  • 3
  • 7
1

This function check the special chars on key press and eliminates the value if it is not a number

function mobilevalid(id) {

    var feild = document.getElementById(id);

    if (isNaN(feild.value) == false) {
        if (feild.value.length == 1) {
            if (feild.value < 7) {
                feild.value = "";
            }
        } else if (feild.value.length > 10) {
            feild.value = feild.value.substr(0, feild.value.length - 1);
        }
        if (feild.value.charAt(0) < 7) {
            feild.value = "";
        }
    } else {
        feild.value = "";
    }

}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
1

If you type:

if { number.value.length!= 10}...     

It will sure work because the value is the quantity which will be driven from the object.

Bugs
  • 4,491
  • 9
  • 32
  • 41
king
  • 11
  • 1
0

Verify this code : It works on change of phone number field in ms crm 2016 form .

function validatePhoneNumber() {

    var mob = Xrm.Page.getAttribute("gen_phone").getValue();
    var length = mob.length;
    if (length < 10 || length > 10) {
        alert("Please Enter 10 Digit Number:");
        Xrm.Page.getAttribute("gen_phone").setValue(null);
        return true;
    }
    if (mob > 31 && (mob < 48 || mob > 57)) {} else {
        alert("Please Enter 10 Digit Number:");
        Xrm.Page.getAttribute("gen_phone").setValue(null);
        return true;
    }
}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Kumar Kirti
  • 61
  • 1
  • 2
0
<script type="text/javascript">
    function MobileNoValidation()
    {
       var phno=/^\d{10}$/
       if(textMobileNo.value=="")
       {
        alert("Mobile No Should Not Be Empty");
       }
       else if(!textMobileNo.value.match(phno))
       {
        alert("Mobile no must be ten digit");
       }
       else
       {
        alert("valid Mobile No");
       }
    }
</script>
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
  • Hi, welcome to Stack Overflow. When answering a question that already has many answers, please be sure to add some additional insight into why the response you're providing is substantive and not simply echoing what's already been vetted by the original poster. This is especially important in "code-only" answers such as the one you've provided. – chb Feb 24 '19 at 10:23
0

I used the follow code.

var mobileNumber=parseInt(no)
  if(!mobileNumber || mobileNumber.toString().length!=10){
  Alert("Please provide 10 Digit numeric value")
}

If the mobile number is not a number, it will give NaN value.

B--rian
  • 5,578
  • 10
  • 38
  • 89
Saurabh Joshi
  • 71
  • 1
  • 8
  • I am reading number from csv sheet so for validation I have used this approach.As people can send string value also .So I parse it and then convert to string for length checking. – Saurabh Joshi Jan 29 '20 at 11:25
  • parseInt('123456ABC') gives output 123456 – Shiva Jun 14 '23 at 13:10
0
<script>
    function validate() {
        var phone=document.getElementById("phone").value;
        if(isNaN(phone))
        {
            alert("please enter digits only");
        }

        else if(phone.length!=10)
        {
            alert("invalid mobile number");
        }
        else
        {
            confirm("hello your mobile number is" +" "+phone);
        }
</script>