Simple phone number validation in JavaScript code example
Example 1: mobile number validation in javascript
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="">
Email:<input type="text" id="number" onkeyup="validation()">
</form>
</body>
<script type="text/javascript">
function validation(){
var number=document.getElementById("number").value;
var numberpattern=/^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
if(numberpattern.test(number))
{
document.getElementById("number").style.backgroundColor='yellow';
}
else
{
document.getElementById("number").style.backgroundColor='red';
}
}
</script>
</html>
Example 2: phone number validation javascript
function telephoneCheck(str) {
var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
return regex.test(str);
}