Created
October 5, 2021 08:27
-
-
Save adarsh-chakraborty/026af61e8cec9626e6a94e0824772c93 to your computer and use it in GitHub Desktop.
Just a demo html page to demonstrate name validation in inputBox by user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Form Validation</title> | |
</head> | |
<body> | |
<input id="fullName" type="text" minlength="4"><button id="btn">Submit</button> | |
<script> | |
const btn = document.getElementById('btn'); | |
const fullName = document.getElementById('fullName'); | |
btn.addEventListener('click',() => { | |
let nameText = fullName.value.trim(); | |
// Check if empty. minimum 4 + 4 | |
if(nameText.length < 8){ | |
return alert("Please enter full name. (Min 4 chars each) <First Name> <Last Name>"); | |
} | |
let name = nameText.split(' '); // Regex blank space (Spliting by space); | |
// We will proceed only if the value has exactly two values, First Name & Last name. Else throw Syntax error | |
if(name.length !== 2){ | |
return alert("Invalid syntax, Please enter only <First Name> <Last Name>"); | |
} | |
if(name[0].trim().length < 4 || name[1].trim().length < 4){ | |
return alert("Minimum 4 characters for each <First Name> <Last Name>"); | |
} | |
// Checking if name contains numbers | |
let letters = /^[A-Za-z]+$/; | |
if(!name[0].match(letters) || !name[1].match(letters)){ | |
return alert("Only letters are allowed in names. [A-Z] <First Name> <Last Name>"); | |
} | |
alert('Congrats! All tests passed, Your name has met all the conditions!'); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment