[go: up one dir, main page]

0% found this document useful (0 votes)
6 views2 pages

Form Validation

formvalidation

Uploaded by

vanitha.cse
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)
6 views2 pages

Form Validation

formvalidation

Uploaded by

vanitha.cse
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/ 2

<html>

<head>
<title>Form Validation</title>
<script type="text/javascript">
function validateForm() {
// Get form values
var name = document.forms["myForm"]["name"].value;
var email = document.forms["myForm"]["email"].value;
var password = document.forms["myForm"]["password"].value;

// Name validation (should not be empty)


if (name == "") {
alert("Name must be filled out");
return false;
}

// Email validation (simple regex for email format)


var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,}$/;
if (email == "") {
alert("Email must be filled out");
return false;
} else if (!emailRegex.test(email)) {
alert("Please enter a valid email address");
return false;
}

// Password validation (minimum 6 characters)


if (password == "") {
alert("Password must be filled out");
return false;
} else if (password.length < 6) {
alert("Password must be at least 6 characters long");
return false;
}

// If all validations pass


alert("Form submitted successfully!");
return true;
}
</script>
</head>
<body>

<h2>Form Validation Example</h2>


<form name="myForm" action="#" onsubmit="return validateForm()">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label><br>
<input type="text" id="email" name="email"><br><br>

<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>

<input type="submit" value="Submit">


</form>

</body>
</html>

You might also like