Form

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

<!DOCTYPE html> <html> <head> <style> .

error { color: red; } </style>


</head> <body> <h1>Form Validation</h1> <form id=”myform” onsub-
mit=”return validateForm()”>
<label for=”name”>Name:</label><br> <input type=”text” id=”name”
name=”name”> <span id=”nameError” class=”error”></span><br>
<label for=”email”>Email:</label><br> <input type=”text” id=”email”
name=”email”> <span id=”emailError” class=”error”></span><br>
<input type=”submit” value=”Submit”> </form>
<script> function validateForm() { var nameInput = document.getElementById(”name”);
var emailInput = document.getElementById(”email”); var nameError
= document.getElementById(”nameError”); var emailError = docu-
ment.getElementById(”emailError”);
nameError.textContent = ””; emailError.textContent = ””;
if (nameInput.value === ””) { nameError.textContent = ”Name is Required”;
nameInput.focus(); return false; }
if(emailInput.value===””){ emailError.textContent = ” email is required”;
emailInput.focus(); return false; }
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(emailInput.value))
{ // Fixed .text to .test emailError.textContent = ”Invalid email”; emailIn-
put.focus(); return false; }
return true; } </script> </body> </html>

You might also like