Skip to content

Instantly share code, notes, and snippets.

@muindetuva
Created March 2, 2023 15:15
Show Gist options
  • Select an option

  • Save muindetuva/8fd0aebb1b72a7ebbd3f92dd80a658b0 to your computer and use it in GitHub Desktop.

Select an option

Save muindetuva/8fd0aebb1b72a7ebbd3f92dd80a658b0 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flex</title>
</head>
<body>
<h1> Example Form</h1>
<form id="form">
<div>
<label for="name">Name</labe>
<input type="text" name="name" id="name">
<div style="color: red;" id="error"></div>
</div>
<div>
<label for="email">Email</label>
<input type="text" name="email" id="email">
</div>
<input type="submit" value="Submit">
</form>
<script>
const form = document.getElementById("form");
let submit = (e) => {
e.preventDefault();
const name = document.getElementById('name');
const email = document.getElementById('email');
const error = document.getElementById('error')
if (nameValidator(name.value)) {
console.log(name.value)
} else {
error.innerText = "Invalid Input for name";
console.log("Invalid input for name")
}
}
let nameValidator = (name) => {
const re = /^[a-zA-Z ]*$/;
return name.match(re);
// console.log(name.match / (re));
}
form.addEventListener('submit', submit)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment