Friday, 22 May 2020

Name and Mobile Number



first.js

function chkForm()
{
//validate form fields
           
/* first step is to fetch all entry values */

idVal=document.getElementById('t1').value;
MobileNumber=document.getElementById('t2').value;
           

/* check id field for required length */
if(idVal.length<5)
{
alert('User name should countain 5 characters');
document.getElementById('t1').value='';
document.getElementById('t1').focus();
return false;
}
// validation of alphabetics
if(idVal)
{
for(var i = 0;i<idVal.length; i++)
{
if(idVal.charAt(i) < 'A' || idVal.charAt(i) > 'Z' && idVal.charAt(i) <'a' || idVal.charAt(i)>'z')
{
alert("Invalit Text,not accept numbers, special characters, alphanumeric characters")
return false;
}
}
}

//validation of mobile number.
           
if(document.getElementById('t2').value =='')
{
alert("Enter 10 digit mobile number");
return false;
}         
           
if(document.getElementById('t2').value != "")
{
var y = document.getElementById('t2').value;

if(isNaN(y)||y.indexOf(" ")!=-1)
{
alert("Invalid Mobile No.");
document.getElementById('mobile_number').focus();
return false;
}
        
if (y.length>10 || y.length<10)
{
alert("Mobile No. should be 10 digit");
document.getElementById('mobile_number').focus();
return false;
}
if (!(y.charAt(0)=="9" || y.charAt(0)=="8"))
{
alert("Mobile No. should start with 9 or 8 ");
document.getElementById('mobile_number').focus();
return false
}
}
return true;
}

Example.html

<html>
<head>

<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="first.js"></script>
</head>
<body>
<table border="1" width="60%">

<form>
<tr><td>Name</td><td><input type="text" name="t1" id="t1"></tr>
<tr><td>Mobile No</td><td><input type="text" name="t2" id="t2"></tr>
<tr><td align="center" colspan="2">
<input type="submit" name="submit" Value="Submit" onclick="return chkForm();" /></td></tr>
</form>
</table>
</body>
</html>