You are on page 1of 4

Experiment : 7

Aim: Write JavaScript to validate the following fields of the


above registration page.
 E-mail id (should not contain any invalid and must follow the standard
pattern(name @domain.com) .
 Phone Number(Phone number should contain 10 digits only).

Objective:
1. Problem Statement: - To write JavaScript to validate the E-mail and the
Phone number field of the Registration Page.

2. Stepwise Solution: -
 Initiate the JS Page by accessing the elements from HTML file using
DOM manipulation.
 Use the logics of JavaScript to validate the E-mail id and Phone
number fields of the Registration page.
 Save the file using .js extension.
 Link the JavaScript file with the HTML file using script tag.
 Run the HTML file on browser (IE / google-chrome / Mozilla-firefox).

Code & Outcome: - Snapshots of the Registration Page.


CODE:

function validatePhone(){
var phone=document.myform.phone.value;
var email=document.myform.email.value;
if(phone==null||phone==""){
alert("Enter Phone Number.");
return false;
}
else if(phone.length!=10){
alert("Phone Number must be of 10 digits.");
return false;
}
else if((/^[0-9]{10}$/).test(phone)==false){
alert("Phone Number must contains only numbers.");
return false;
}
else if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+
$/.test(email)==false){
alert("Invalid Email-id.");
return false;
}
}
OUTPUT:

You might also like