Hi,
I'm new to javascript that i didn't learn in college. My form can validate the first name, last name, and address line 1, but cannot validate the phone number and e-mail address.
Can u help me to fix this?
<!DOCTYPE html> <html> <head> <title>Plain form</title> <script>
function validateForm() {
// First name
var fn = document.forms["form1"]["firstName"].value;
if (fn == null || fn == "") {
alert("First name must be filled out");
return false;
}
// Last name
var ln = document.forms["form1"]["lastName"].value;
if (ln == null || ln == "") {
alert("Last name must be filled out");
return false;
}
// Phone number
var phoneno = /^\d{11}$/;
if((pNo.value.match(phoneno))
{
return true;
}
else
{
alert("Invalid phone number! Digits only!");
return false;
}
// E-mail address
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(emailAdd.value.match(mailformat))
{
document.form1.emailAdd.focus();
return true;
}
else
{
alert("You have entered an invalid email address!");
document.form1.emailAdd.focus();
return false;
}
// Address Line 1
var a1 = document.forms["form1"]["addLine1"].value;
if (a1 == null || a1 == "") {
alert("First name must be filled out");
return false;
}
}
</script> </head> <body> <p>Fill up all the details in this form below, then click Submit. Required fields are marked in asteriks.</p> <form id="form1" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="firstName">*<br>
Last name: <input type="text" name="lastName">*<br>
Phone number: <input type="text" name="pNo" maxlength="11">*<br>
E-mail address: <input type="text" name="emailAdd">*<br>
Address Line 1: <input type="text" name="addLine1">*<br>
Address Line 2: <input type="text" name="addLine2"><br> <input type="submit" value="Submit"> </form> </body> </html>