0% found this document useful (0 votes)
2 views

Exp 12

This is notes provided by our teacher

Uploaded by

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

Exp 12

This is notes provided by our teacher

Uploaded by

ansarisadeem8879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Midhat Ansari Roll no: 210403

Sub: Client-Side Scripting (CSS) Branch: CO

Experiment No. 12: Develop a webpage for validation of form fields using
regular expressions
1. Write a JavaScript program to validate Name, Age, Date of Birth, Email Id and
Password using regular expression.
CODE:
<html>
<head>
<title></title>
</head>
<body >
<form name="myForm">
Name: &nbsp; <input type="text" id="name" onblur="validateName()">
<br><br>
Age: &nbsp; <input type="text" id="age" onblur="validateAge()"><br><br>
Date of Birth: &nbsp; <input type="text" id="dob"
onblur="validateDOB()"><br><br>
E-mail ID: &nbsp; <input type="text" id="email"
onblur="validateEmail()"><br><br>
Password: &nbsp; <input type="text" id="password"
onblur="validatePassword()"><br><br>
Midhat Ansari-210403
</form>
<script>
function validateName() {
var regex = /^[a-zA-Z]{2,30}$/;
var name = document.getElementById("name").value;
if(regex.test(name)) {
alert("Successful.");
} else {
alert("Name should not contain special character or digits.");
}
}
function validateAge(){
var regex = /^(0?[1-9]|[1-9][0-9])$/;
var age = document.getElementById("age").value;
if(regex.test(age) && age >= 18) {
alert("Successful.");
} else {
alert("Age should be in between 18 and 99");
}
}
function validateDOB(){
var regex = /^(0[1-9]|[12][1-9]|3[01])[./\-](0[1-9]|1[102])[./\-](19|20)\d\d$/;
var dob = document.getElementById("dob").value;
if(regex.test(dob)) {
alert("Successful.");
} else {
alert("Formats: dd-mm-yyyy, dd/mm/yyy, dd.mm.yyyy");
}
}
function validateEmail(){
var regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var email = document.getElementById("email").value;
if(regex.test(email)) {
alert("Successful");
} else {
alert("Invalid E-mail!!");
}
}
function validatePassword(){
var regex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]
{6,16}$/;
var password = document.getElementById("password").value;
if(regex.test(password)) {
alert("Successful.");
} else {
alert("Must contain atleast 1 uppercase, 1 lowercase, 1 digit, 1 special-
character and length between 6-16");
}
}
</script>
</body>
</html>
OUTPUT:
2. Write a JavaScript program to validate the IP Address using regular expression.

CODE:
<html>
<body>
<form>
<label>IP:</label>
<input type="text" id="myIP" onblur="validateIP()" size="30"/><br>
Midhat Ansari-210403
</form><script>
function validateIP() {
var ip = document.getElementById('myIP').value
var regex = /^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-
9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])$/
if(regex.test(ip)==true) {
alert('Successful')
} else {
alert('Invalid IP')
}}</script></body></html>

OUTPUT:

You might also like