Week 3: Validation: Write Javascript To Validate The Following Fields of The Above Registration Page
Week 3: Validation: Write Javascript To Validate The Following Fields of The Above Registration Page
VALIDATION:
Write JavaScript to validate the following fields of the above registration page.
(i) Name (Name should contain alphabets and the length should not be less than 6 characters).
(ii) Password (Password should not be less than 6 characters length).
(iii) E-mail id (should follow the standard pattern [email protected])
(iv) Phone number (Phone number should contain 10 digits only).
(i) Name
(Name field should contain alphabets & the length should not be less than 6 characters).
name.html
<html>
<head>
<title>
Validations
</title>
<script language="javascript">
function isValidation()
{
var uname=document.getElementById('uname');
if(isalphabet(uname,'Please enter alphabets only'))
if(restrictlengthname(uname,6))
return true;
else
return false;
}
function isalphabet(elem,helpermsg)
{
var alphaexp=/^[a-z A-Z]+$/;
if(elem.value.match(alphaexp))
{
return true;
}
else
{
alert(helpermsg);
elem.focus();
return false;
}
}
function restrictlengthname(elem,min)
{
var uinput=elem.value;
if(uinput.length>=min)
{
return true;
}
else
{
alert("Please enter atleast " +min+ "characters");
elem.focus();
return false;
}
}
</script>
</head>
<body bgcolor="pink">
<h2>Validations</h2>
<form>
Name:
<input type ="text" name="name" id='uname'> <br><br>
<input type="submit" value="Submit" onclick="isValidation()"/>
<input type="reset" value="Reset">
</form>
</body>
</html>
OUTPUT
return true;
else
return false;
}
function restrictlengthname(elem,min)
{
var uinput=elem.value;
if(uinput.length>=min)
{
return true;
}
else
{
alert("Please enter atleast " +min+ "characters");
elem.focus();
return false;
}
}
</script>
</head>
<body bgcolor="pink">
<h2>Validations</h2>
<form>
Password:
<input type ="password" name="password" id='pwd'> <br><br>
OUTPUT
function isValidation()
{
var mail=document.getElementById('mail');
if(emailvalidation(mail,'Please enter a valid email id'))
return true;
else
return false;
}
function emailvalidation(elem,helpermsg)
{
var emailexp=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(elem.value.match(emailexp))
{
return true;
}
else
{
alert(helpermsg);
elem.focus();
return false;
}
}
</script>
</head>
<body bgcolor="pink">
<h2>Validations</h2>
<form>
Email ID:
<input type ="text" name="email" id='mail'> <br><br>
<input type="submit" value="Submit" onclick="isValidation()"/>
<input type="reset" value="Reset">
</form>
</body>
</html>
OUTPUT
<title>
Validations
</title>
<script language="javascript">
function isValidation()
{
var ph=document.getElementById('ph');
if(isnumeric(ph,'Please enter digits only'))
if(restrictlength(ph,10))
return true;
else
return false;
}
function isnumeric(elem,helpermsg)
{
var numexp=/^[0-9]+$/;
if(elem.value.match(numexp))
{
return true;
}
else
{
alert(helpermsg);
elem.focus();
return false;
}
}
function restrictlength(elem,min)
{
var uinput=elem.value;
if(uinput.length==min)
{
return true;
}
else
{
alert("Please enter " +min+ "digits");
elem.focus();
return false;
}
}
</script>
</head>
<body bgcolor="pink">
<h2>Validations</h2>
<form>
Phone:
<input type ="text" name="phone" id='ph'> <br><br>
<input type="submit" value="Submit" onclick="isValidation()"/>
<input type="reset" value="Reset">
</form>
</body>
</html>
OUTPUT
return true;
else
return false;
}
function isalphabet(elem,helpermsg)
{
var alphaexp=/^[a-z A-Z]+$/;
if(elem.value.match(alphaexp))
{
return true;
}
else
{
alert(helpermsg);
elem.focus();
return false;
}
}
function restrictlengthname(elem,min)
{
var uinput=elem.value;
if(uinput.length>=min)
{
return true;
}
else
{
}
else
{
alert(helpermsg);
elem.focus();
return false;
}
}
function restrictlength(elem,min)
{
var uinput=elem.value;
if(uinput.length==min)
{
return true;
}
else
{
alert("Please enter " +min+ "digits");
elem.focus();
return false;
}
}
function emailvalidation(elem,helpermsg)
{
var emailexp=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(elem.value.match(emailexp))
{
return true;
}
else
{
alert(helpermsg);
elem.focus();
return false;
}
}
</script>
</head>
<body bgcolor="pink">
<center>
<h1>REGISTRATION</h1>
</center>
<form>
<table border="0">
<tr>
<td>Name:</td>
<td><input type ="text" name="name" id='uname'></td>
</tr>
<tr>
<td>Password:</td>
</tr>
</table>
</form>
</body>
</html>
OUTPUT