Check javascript cell number

I want to check the 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 problem, when I submit the form without entering the phone number, it shows that the error cell number should not be zero. It works great.

When I submit a form with a cell number less than 10 digits, it shows that the phone number should be 10 digits. It is also beautiful.

The problem is that I submit the form with 10 digits and then shows the error phone number should be 10 digits.

Please help me. Thanks.

And you also need a verification code for cell numbers only.

+6
source share
7 answers

If number is your form element, then its length will be undefined since the elements have no length. Do you want to

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

An easier way to do the whole check at once would be with a regular expression:

 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. ^ and $ bind it to the beginning and end, so something like asdf1234567890asdf does not match.

+15
source
 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; } 

Mobile Phone Number Verification Function HTML Code -

+4
source

Checking your mobile number using Java Script This link will provide a demo and additional information.

 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();"> 
+1
source

Check this code: It works with changing the 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; } } 
0
source

This function checks for a special press of the chars key and excludes the value if it is not 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 = ""; } } 
0
source

If you type:

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

It will work because the value is the amount to be deduced from the object.

0
source
 <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> 
0
source

All Articles