Step of Name Validation
- Create an HTML form
- Write the validation code
Create an HTML form
<!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>Rays Coding-JQuery Validation</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container mt-5 form_box">
<div class="row">
<div class="card p-0">
<div class="card-header bg-primary">
<h3 class="text-center text-white">Rays Coding :- JQuery Form Validation(Password)</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-lg-12">
<form action="#" method="post" enctype="multipart/form-data" id="form_data">
<div class="row">
<div class="col-lg-12">
<div class="mb-3">
<label for="userpassword">Password</label>
<input type="password" name="user_password" class="form-control" id="userpassword" placeholder="Enter Your Password" autocomplete="off">
<span id="password_error" class="passworderror"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<button type="submit" class="btn btn-primary mt-4" name="sub_btn" id="sub_button">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12 validationtext">
<h1>jQuery Password Validation</h1>
</div>
</div>
</div>
<!-- jquery CDN -->
<script src="https://code.jquery.com/jquery-3.6.2.slim.min.js" integrity="sha256-E3P3OaTZH+HlEM7f1gdAT3lHAn4nWBZXuYe89DFg2d0=" crossorigin="anonymous"></script>
</body>
</html>
Write the validation code
<script>
$(document).ready(function () {
$('#sub_button').on('click', function (e) {
e.preventDefault();
var field_val = $('#userpassword').val();
let uppercase = /[A-Z]/g;
let lowercase = /[a-z]/g;
let digit = /[0-9]/g;
let special_car = /[!@#$%^&*()_]/g;
if (field_val == '') {
console.log(field_val);
$('#password_error').text('Password is requried');
$('#password_error').css('color', 'crimson');
$('#userpassword').focus();
return false;
}
else if (!field_val.match(uppercase)) {
console.log(field_val);
$('#password_error').text('Please enter at least one capital letter.');
$('#password_error').css('color', 'crimson');
$('#userpassword').focus();
return false;
}
else if (!field_val.match(lowercase)) {
console.log(field_val);
$('#password_error').text('Please enter at least one small letter.');
$('#password_error').css('color', 'crimson');
$('#userpassword').focus();
return false;
}
else if (!field_val.match(digit)) {
console.log(field_val);
$('#password_error').text('Please enter at least one digit.');
$('#password_error').css('color', 'crimson');
$('#userpassword').focus();
return false;
}
else if (!field_val.match(special_car)) {
console.log(field_val);
$('#password_error').text('Please enter at least one special character.');
$('#password_error').css('color', 'crimson');
$('#userpassword').focus();
return false;
}
else if (field_val.length < 8) {
console.log(field_val);
$('#password_error').text('Please enter minimum eight character.');
$('#password_error').css('color', 'crimson');
$('#userpassword').focus();
return false;
}
else {
console.log(field_val);
console.log('complete');
$('#password_error').text('');
$('#form_data').trigger('reset');
}
});
});
</script>
- If the password field is empty, it displays an error message indicating that the password is required.
- If the password does not contain at least one uppercase letter, it prompts the user to include one.
- If the password does not contain at least one lowercase letter, it prompts the user to include one.
- If the password does not contain at least one digit, it prompts the user to include one.
- If the password does not contain at least one special character, it prompts the user to include one.
- If the password length is less than 8 characters, it prompts the user to use a longer password.
- For each validation condition that fails, an error message is displayed, the text color is set to crimson (a shade of red), and the focus is returned to the password input field to facilitate correction.