xxxxxxxxxx
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const email = document.getElementById('email').value;
let isValid = true;
if (email.indexOf('@') === -1) {
console.log('Invalid email address: "@" is missing.');
isValid = false;
}
if (username.indexOf(' ') !== -1) {
console.log('Invalid username: spaces are not allowed.');
isValid = false;
}
if (password.length < 8) {
console.log('Invalid password: must be at least 8 characters long.');
isValid = false;
}
if (password.indexOf(username) !== -1) {
console.log('Invalid password: the password should not contain the username.');
isValid = false;
}
// Use regular expressions for more complex validations
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
console.log('Invalid email format.');
isValid = false;
}
const usernameRegex = /^\S+$/; // No spaces allowed
if (!usernameRegex.test(username)) {
console.log('Invalid username: spaces are not allowed.');
isValid = false;
}
if (isValid) {
console.log('Form is valid!');
// Submit form or further processing
} else {
console.log('Form is invalid.');
}
HTML and Javasript
xxxxxxxxxx
//HTML
<form action="#" name="PracticeForm" onsubmit="return formValidation()" method="post" id="FormID">
<div class="form">
<div id="formName">
<label for="fname">Name:</label>
<input type="text" name="fname"><b><span class="error" style="color:red"></span></b>
</div><br>
<div id="formEmail">
<label for="femail">Email:</label>
<input type="text" name="femail"><b><span class="error" style="color:red"></span></b>
</div>
<br>
<div class="submit_btn">
<input type="submit" value="Submit" id="submit_btn">
</div>
</div>
</form>
//Javascript
function clearErrors() {
error = document.getElementsByClassName('error');
for (let item of error){
item.innerHTML = '';
}
}
function setErrorMessage(id, ErrorMessage){
element = document.getElementById(id);
element.getElementsByClassName("error")[0].innerHTML = ErrorMessage;
}
function formValidation(){
var returnval = true;
clearErrors();
var name = document.forms['PracticeForm']['fname'].value;
if (name.length != 11){
setErrorMessage('formName', "*Please Enter Valid Name!!");
returnval = false;
}
var email = document.forms['PracticeForm']['femail'].value;
if (email.length != 11){
setErrorMessage('formEmail', "*Please Enter Valid Email!!");
returnval = false;
}
return returnval;
}
xxxxxxxxxx
<form id="myForm" onsubmit="return validateForm()">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="text" id="email" name="email"><br>
<input type="submit" value="Submit">
</form>
<script>
function validateForm() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var errorMessages = [];
// Check if name is empty
if (name.trim() === "") {
errorMessages.push("Name is required");
}
// Check if email is empty and if it's a valid email format
if (email.trim() === "") {
errorMessages.push("Email is required");
} else if (!isValidEmail(email)) {
errorMessages.push("Invalid email format");
}
// Display error messages if any
if (errorMessages.length > 0) {
alert(errorMessages.join("\n"));
return false; // Prevent form submission
}
return true; // Allow form submission
}
// Function to validate email format
function isValidEmail(email) {
var emailRegex = /\S+@\S+\.\S+/;
return emailRegex.test(email);
}
</script>
xxxxxxxxxx
let input_value = document.forms["myForm"]["myInputField"].value;
if(input_value == undefined || input_value.trim() == "") {
console.log("InputName is required")
}