How to check phone number using javascript?

Will someone a little smarter than me be able to help me with this feature? Its purpose is to check the text input in the form, the phone number field, which will only accept 0-9, dashes and periods. HTML calls the fine function.

function validateFeedback() { var phone = document.getElementById("phone"); var validNumber = "0123456789.-"; for (i = 0; i < phone.length; i++); { if (validNumber.indexOf(phone.charAt(i)) == -1); { alert("You have entered an invalid phone number"); return false; } } return true; } 

Thanks so much for any help.

+4
source share
7 answers

Regular expressions should help;)

Sorry, I have not tried running this code, but everything should be fine.

 function validateFeedback(){ var phone = document.getElementById("phone"); var RE = /^[\d\.\-]+$/; if(!RE.test(phone.value)) { alert("You have entered an invalid phone number"); return false; } return true; } 
+8
source

try like this:

 function validateFeedback() { var phone = document.getElementById("phone"); var validNumber = "0123456789.-"; for(i = 0; i < phone.length; i++) { if(validNumber.indexOf(phone.charAt(i)) == -1) { alert("You have entered an invalid phone number"); return false; } } return true; } 

there is ; out of place ...

+2
source

Try this one

 function validateFeedback(value) { var length = value.length; chk1="1234567890()-+ "; for(i=0;i<length;i++) { ch1=value.charAt(i); rtn1=chk1.indexOf(ch1); if(rtn1==-1) return false; } return true; } 
+1
source

I think you should use regex for this. Something to relate to this:

 function validateFeedback() { var phone = document.getElementById("phone").value; var reg = new RegExp("[0-9 .-]*"); return reg.test(phone); } 
+1
source

If the text input is on the form, you can refer to it more directly using the form identifier and element identifier:

 var phone = document.<formId>.phone; 

What you want to check is the value of the element, so you need to:

 var phone = document.<formName>.phone.value; 

Since the function is probably called from the submit listener on the form, you can make things more efficient by using:

 <form onsubmit="return validateFeedback(this);" ...> 

It also seems to me that the phone number has only numbers, not a "-" or ".". characters, so you should only check the numbers 0-9.

So the function could be like this:

 function validateFeedback(form) { var phoneValue = form.phone.value; // Use a regular expression to validate the value // is only digits if (/\D/.test(phoneValue) { // value contains non-digit characters // advise user of error then return false; } } 

you can check that the length is also reasonable, but note that phone numbers in different places of different lengths, depending on the location and use of the area code or country, and whether the number is for mobile, landline or others.

+1
source

I would rather use regular expressions for something like this. Check out my modified version of your feature, which should work in all major browsers without any frameworks.

 function validateFeedback() { // Get input var phone = document.getElementById("phone"), // Remove whitespaces from input start and end phone = (phone || '').replace(/^\s+|\s+$/g, ''), // Defined valid charset as regular expression validNumber = "/^[0123456789.-]+$/"; // Just in case the input was empty if (phone.length == 0) { // This depends on your application - is an empty number also correct? // If not, just change this to "return false;" return true; } // Test phone string against the regular expression if (phone.match(validNumber)) { return true; } // Some invalid symbols are used return false; } 
+1
source
 function phonenumber(inputtxt) { var phoneno = /^\d{10}$/; if((inputtxt.value.match(phoneno)) { return true; } else { alert("message"); return false; } } 
+1
source

Source: https://habr.com/ru/post/1412753/


All Articles