
in the web form the phonenumber field should be like this format xxx-xxx-xxxx, i want the code in javascript. as a function.

Javascript Phone Number Validation
<html>
<script>
function validate(){
var ph=document.getElementById('phone').value;
var regExpObj = /(\d\d\d)-(\d\d\d)-(\d\d\d\d)/;
if(ph==""){
alert("Enter Phone Number!");
}
else if(regExpObj.exec(ph) == null){
alert('Invalid Phone Number!');
return false;
} else {
return true;
}
}
</script>
<form id="form" method="post" onsubmit="javascript:return validate();">
Phone Number:<input type="text" id="phone"><br>
<input type="submit" value="Submit">
</form>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.