With this article, I am starting a series on form validation. In this validation series, I will give information about implementing validation using jQuery. But before understanding validation, I will tell you what validation is, why validation is necessary and what are the scenarios of validation of those input fields which are used a lot in the form.
We know that there are different types of input boxes in a form, and there are different scenarios to validate each input box. In this article, I will give you information about “Name Validation” as well as all these scenarios.
What is validation
Why need for validation
Validation Scenarios
- Input box contains the value or not (required).
- Length of the value.
- The name should contain alphabetic characters, no numeric values or special characters.
- The email field is not empty (required field)
- The email format is valid
- Value exists or not (required field)
- Value length (minimum and maximum length)
- No alphabetic characters or special characters
- Invalid number (contains alphabetic or special characters):
- Decimal and integer validation
- Mandatory Selection: The user must select the radio button.
- Mandatory selection (at least one checkbox)
- Maximum/minimum selection limits
- Mandatory date field
- Invalid date format
- Minimum length validation
- Maximum length validation
- Password strength validation
- Blanks validation
- Pattern matching (regular expression)
- Required Field Validation
- Valid Phone Number Format (Specific to Country):
- Phone Number Length Validation
- Required Field Validation
- File Type Validation
- File Size Validation
- Multiple Files Upload Validation
- Validating the Number of Files
- Required Field Validation
- Formate validate
Name Validation
- The field is required
- Min and Max length
- Regex validation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RAYS CODING : Name Validation</title>
<link rel="stylesheet" href="../style.css">
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-zinc-950 flex justify-center">
<section class="w-auto h-auto mt-28 bg-white">
<header class="bg-violet-600 p-5 pl-6 pr-6">
<h2 class="text-3xl text-white tracking-wider">RAYS CODING : jQuery Name Validation</h2>
</header>
<form action="#" method="post" class="p-5">
<input type="text" id="username"
class="w-full h-12 p-3 border-2 border-violet-600 focus:outline-none focus:border-violet-600 hover:border-violet-600"
placeholder="Enter Your Name">
<div class="mt-5 bg-red-300 p-3 text-red-950 hidden" id="error-message"></div>
<button type="submit" class="mt-5 bg-violet-600 p-3 w-full text-white font-semibold tracking-wider"
id="submit-button">SUBMIT</button>
</form>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</body>
</html>
Validation in jQuery
<script>
$(document).ready(function () {
$('#submit-button').on('click', function (e) {
e.preventDefault();
let input_val = $('#username').val();
let name_regexp = /^[a-zA-Z\s]+$/;
if (input_val === '') {
$('#error-message').removeClass('hidden').text('Please enter your name, name field is required.').fadeIn().delay(3000).fadeOut(function () {
$(this).addClass('hidden');
return false;
});
} else if (input_val.length < 3) {
$('#error-message').removeClass('hidden').text('Name must be at least 3 characters long. Please enter a valid name.').fadeIn().delay(3000).fadeOut(function () {
$(this).addClass('hidden');
return false;
});
} else if (input_val.length > 50) {
$('#error-message').removeClass('hidden').text('Name cannot be more than 50 characters. Please shorten your name.').fadeIn().delay(3000).fadeOut(function () {
$(this).addClass('hidden');
return false;
});
} else if (!input_val.match(name_regexp)) {
$('#error-message').removeClass('hidden').text('Please enter a valid name. Only alphabetic characters and spaces are allowed.').fadeIn().delay(3000).fadeOut(function () {
$(this).addClass('hidden');
return false;
});
} else {
$('#error-message').text(input_val).removeClass('hidden bg-red-300 text-red-950').addClass('bg-green-300 text-green-950').fadeIn().delay(3000).fadeOut(function () { $(this).addClass('hidden'); return true; });
}
});
});
</script>