Phone and Email Validation – Javascript

Phone and Email Address validation using Javascript

The below example with source code explains how to validate phone/mobile number and email address.

Source code: [Create the following program in an editor]

<html>
<head>
<script type=”text/javascript”>
function Validation()
{
var a=document.form.phone.value;
var b=document.form.email.value;
if(a==””)
{
alert(“please Enter the Contact Number”);
document.form.phone.focus();
return false;
}
if(isNaN(a))
{
alert(“Enter the valid Mobile Number(Like : 9876543210)”);
document.form.phone.focus();
return false;
}
if(a.length!=10)
{
alert(” Your Mobile Number must be 10 Integers”);
document.form.phone.select();
return false;
}

len=b.length
for(i=2; i<len; i++)
{
if(b.charAt(i)==’@’)
{ return true}
}
alert(“Invalid EmailId”)
return false

}
</script>
</head>
<body>
<form name=”form” method=”post” onsubmit=”return
Validation()”>
<table border=1 style=”background-color:khaki; border-radius:5px
5px 5px 5px ” align=”center”>
<tr>
<td style=”background-color:cyan”> Mobile No:</td>
<td><input type=”text” name=”phone”
style=”background-color:yellow; border-radius:5px 5px 5px 5px”>
</td>
</tr>

<tr>
<td style=”background-color:cyan”> Email ID:</td>
<td><input type=”text” name=”email”
style=”background-color:yellow; border-radius:5px 5px 5px
5px”></td>
</tr>

<tr>
<td></td>
<td><input type=”submit” name=”sub” value=”Submit”
style=”background-color:green; color:white; border-radius:15px
15px 15px 15px”></td>
</tr>
</form>
</body>
</html>

 

And to see an output as following in a browser.

Phone and Email validation

also, read on Internet of Things [IoT] Here

Share
Share
Scroll to Top