0% found this document useful (0 votes)
18 views3 pages

Course

Uploaded by

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

Course

Uploaded by

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

<!

DOCTYPE html>

<html>

<head>

<title>Form Validation Example</title>

<script>

function validateForm() {

// Retrieve form input values

var name = document.getElementById('name').value.trim();

var email = document.getElementById('email').value.trim();

var password = document.getElementById('password').value.trim();

var course = document.getElementById('course').value;

// Check if name is empty

if (name === '') {

alert('Please enter your name.');

return false;

// Check if email is empty or not valid

if (email === '') {

alert('Please enter your email address.');

return false;

} else if (!validateEmail(email)) {

alert('Please enter a valid email address.');

return false;

// Check if password is empty or less than 8 characters

if (password === '') {

alert('Please enter your password.');

return false;
} else if (password.length < 8) {

alert('Password must be at least 8 characters long.');

return false;

// Check if a course is selected

if (course === 'select') {

alert('Please select a course.');

return false;

// If all validations pass, submit the form

return true;

function validateEmail(email) {

// Regular expression pattern for email validation

var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

return pattern.test(email);

</script>

</head>

<body>

<h1>Form Validation Example</h1>

<form onsubmit="return validateForm()">

<label for="name">Name:</label>

<input type="text" id="name">

<br><br>

<label for="email">Email:</label>

<input type="email" id="email">


<br><br>

<label for="password">Password:</label>

<input type="password" id="password">

<br><br>

<label for="course">Select Course:</label>

<select id="course">

<option value="select">--Select a Course--</option>

<option value="web_dev">Web Development</option>

<option value="app_dev">App Development</option>

<option value="digital_marketing">Digital Marketing</option>

<option value="iot">IoT Development</option>

<option value="ecommerce">E-Commerce</option>

</select>

<br><br>

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

</form>

</body>

</html>

You might also like