0% found this document useful (0 votes)
4 views20 pages

Validation Program

The document provides JavaScript code for validating a registration form with fields for name, password, email, and phone number. Each field has specific validation rules, such as minimum character length for name and password, a standard email format, and a 10-digit requirement for phone numbers. The code includes functions for checking these validations and alerts the user if any input does not meet the criteria.

Uploaded by

SACHIN VERMA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Validation Program

The document provides JavaScript code for validating a registration form with fields for name, password, email, and phone number. Each field has specific validation rules, such as minimum character length for name and password, a standard email format, and a 10-digit requirement for phone numbers. The code includes functions for checking these validations and alerts the user if any input does not meet the criteria.

Uploaded by

SACHIN VERMA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week 3

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 name@[Link])

(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).

[Link]

<html>

<head>

<title>

Validations

</title>

<script language="javascript">

function isValidation()

var uname=[Link]('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([Link](alphaexp))

{
return true;

else

alert(helpermsg);

[Link]();

return false;

function restrictlengthname(elem,min)

var uinput=[Link];

if([Link]>=min)

return true;

else

alert("Please enter atleast " +min+ "characters");

[Link]();

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
(ii) Password (Password should not be less than 6 characters length).

[Link]

<html>

<head>

<title>

Validations

</title>

<script language="javascript">

function isValidation()

var pwd=[Link]('pwd');

if(restrictlengthname(pwd,6))
return true;

else

return false;

function restrictlengthname(elem,min)

var uinput=[Link];

if([Link]>=min)

return true;

else

alert("Please enter atleast " +min+ "characters");

[Link]();

return false;

</script>

</head>

<body bgcolor="pink">

<h2>Validations</h2>

<form>

Password:

<input type ="password" name="password" id='pwd'> <br><br>


<input type="submit" value="Submit" onclick="isValidation()"/>

<input type="reset" value="Reset">

</form>

</body>

</html>

OUTPUT

(iii) E-mail id (should follow the standard pattern name@[Link])

[Link]

<html>

<head>

<title>

Validations

</title>

<script language="javascript">
function isValidation()

var mail=[Link]('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([Link](emailexp))

return true;

else

alert(helpermsg);

[Link]();

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

(iv) Phone number (Phone number should contain 10 digits only).

[Link]

<html>

<head>
<title>

Validations

</title>

<script language="javascript">

function isValidation()

var ph=[Link]('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([Link](numexp))

return true;

else

alert(helpermsg);

[Link]();

return false;
}

function restrictlength(elem,min)

var uinput=[Link];

if([Link]==min)

return true;

else

alert("Please enter " +min+ "digits");

[Link]();

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
Registration form with validations

<html>

<head>

<title>

Registration

</title>

<script language="javascript">

function isValidation()

var uname=[Link]('uname');

var pwd=[Link]('pwd');

var ph=[Link]('ph');

var mail=[Link]('mail');

if(isalphabet(uname,'Please enter alphabets only'))

if(restrictlengthname(uname,6))

if(restrictlengthpwd(pwd,6))

if(isnumeric(ph,'Please enter digits only'))

if(restrictlength(ph,10))

if(emailvalidation(mail,'Please enter a valid email id'))

return true;

else
return false;

function isalphabet(elem,helpermsg)

var alphaexp=/^[a-z A-Z]+$/;

if([Link](alphaexp))

return true;

else

alert(helpermsg);

[Link]();

return false;

function restrictlengthname(elem,min)

var uinput=[Link];

if([Link]>=min)

return true;

else

{
alert("Please enter atleast " +min+ "characters");

[Link]();

return false;

function restrictlengthpwd(elem,min)

var uinput=[Link];

if([Link]>=min)

return true;

else

alert("Please enter atleast " +min+ "characters");

[Link]();

return false;

function isnumeric(elem,helpermsg)

var numexp=/^[0-9]+$/;

if([Link](numexp))

return true;
}

else

alert(helpermsg);

[Link]();

return false;

function restrictlength(elem,min)

var uinput=[Link];

if([Link]==min)

return true;

else

alert("Please enter " +min+ "digits");

[Link]();

return false;

function emailvalidation(elem,helpermsg)

var emailexp=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if([Link](emailexp))

return true;

else

alert(helpermsg);

[Link]();

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>
<td><input type="password" name="pwd" id='pwd'></td>

</tr>

<tr>

<td>Phone No :</td>

<td><input type="text" name="phone" id='ph'></td>

</tr>

<tr>

<td>E-mail:</td>

<td><input type="text" name="mail" id='mail'></td>

</tr>

<tr>

<td>Gender :</td>

<td><input type="radio" name="group1" value="Male"> Male

<input type="radio" name="group1" value="Female"> Female</td>

</tr>

<tr>

<td>Date of Birth :</td>

<td> <select name="Day">

<option value="1"> 1 </option>

<option value="2"> 2 </option>

<option value="3"> 3 </option>

<option value="4"> 4 </option>

</select>

<select name="Month">

<option value="Jan"> January </option>


<option value="Feb"> February </option>

<option value="Mar"> March </option>

<option value="Apr"> April </option>

</select>

<select name="Year">

<option value="1"> 1991 </option>

<option value="2"> 1992 </option>

<option value="3"> 1993 </option>

<option value="4"> 1994 </option>

</select></td>

</tr>

<tr>

<td>Languages Known :</td>

<td><input type = "checkbox" name="option1" value="English"> English

<input type = "checkbox" name="option2" value="Urdu"> Urdu

<input type = "checkbox" name="option3" value="Telugu"> Telugu

<input type = "checkbox" name="option4" value="Hindi"> Hindi</td>

</tr>

<tr>

<td> Address :</td>

<td><textarea cols="40" rows="4" name="address"> </textarea></td>

</tr>

<tr>

<td><input type="submit" value="Submit" onclick="isValidation()"/>

<input type="reset" value="Reset"></td>


</tr>

</table>

</form>

</body>

</html>

OUTPUT

You might also like