0% found this document useful (0 votes)
4 views1 page

Form Validation Script Guide

The document contains an HTML form with JavaScript validation for user input. It checks for valid name, email, mobile number, gender selection, and at least one selected hobby before submission. Alerts are displayed for any validation errors, and a success message is shown upon successful validation.

Uploaded by

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

Form Validation Script Guide

The document contains an HTML form with JavaScript validation for user input. It checks for valid name, email, mobile number, gender selection, and at least one selected hobby before submission. Alerts are displayed for any validation errors, and a success message is shown upon successful validation.

Uploaded by

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

<html>

<head>
<script>
function validate() {
var form = [Link]["myForm"];
var name = [Link]["name"].[Link]();
var email = [Link]["email"].[Link]();
var mobile = [Link]["mobile"].[Link]();
var gender = [Link]["gender"].value;
var hobby1 = [Link]["hobby1"].checked;
var hobby2 = [Link]["hobby2"].checked;
if (!/^[a-zA-Z ]+$/.test(name)) {
alert("Please enter a valid name (only letters and spaces allowed).");
return false;
}
if (!/^\S+@\S+\.\S+$/.test(email)) {
alert("Please enter a valid email address.");
return false;
}
if (!/^\d{10}$/.test(mobile)) {
alert("Please enter a valid 10-digit mobile number.");
return false;
}
// Validation for Gender
if (!gender) {
alert("Please select a gender.");
return false;
}
// Validation for Hobbies
if (!hobby1 && !hobby2) {
alert("Please select at least one hobby.");
return false;
}
alert("Successfully submitted");
return true;
}

</script>
</head>
<body>
<h1 > Form Validation </h1>
<form name="myForm" onsubmit="return validate()">
Name:
<input type="text" name="name"/><br><br>
Email:
<input type="text" name="email"/><br><br>
Mobile:
<input type="text" name="mobile"/><br><br>
Gender:
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female<br><br>
Hobbies:
<input type="checkbox" name="hobby1" value="reading"> Reading
<input type="checkbox" name="hobby2" value="traveling"> Traveling<br><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>

You might also like