When it comes to web form validation, especially for sensitive information like email addresses and mobile numbers, ensuring that the data entered by users is valid and properly formatted is essential. Client-side validation helps reduce errors and enhances the user experience by providing immediate feedback.
In this article, we’ll break down how to implement comprehensive email and mobile number validation using jQuery. The provided solution validates both email formats and mobile number formats, with added checks for things like sequential digits, keyboard patterns, and repeated characters. Let’s walk through the code and its functionality.
Key Features of the Solution
Mobile Number Validation
The number must be exactly 10 digits.
The number must start with a digit between 7 and 9.
Disallows numbers with identical digits (e.g.,
1111111111).Disallows ascending or descending sequences (e.g.,
1234567890,0987654321).
Email Validation
The email must follow the general email format (e.g.,
[email protected]).Disallows repeated characters in the local part (e.g.,
[email protected]).Checks for sequential characters (e.g.,
[email protected]).Detects repeating patterns within the local part of the email (e.g.,
[email protected]).Prevents keyboard patterns (e.g.,
[email protected]).
Dynamic Form Validation
The form is validated when the user interacts with the form fields (via
changeevents).Provides immediate feedback during user input (via onblur, onchange, and onkeypress events).
The form only submits if both the email and mobile number fields are valid.
Understanding the Code
Here is the full JavaScript solution that performs the validation logic on both email and mobile number fields.
Step 1: jQuery Document Ready
$(document).ready(function () {
// Use 'change' event for validation
$("#<%= txtemail1.ClientID %>").change(function () {
validateEmail(this);
});
$("#<%= txtcontactno1.ClientID %>").change(function () {
validateMobile(this);
});
// Handle form submission validation
$("#<%= btnsignup1.ClientID %>").click(function () {
return validateForm();
});
$("#<%= EMaildId.ClientID %>").change(function () {
validateEmail(this);
});
$("#<%= MobileID.ClientID %>").change(function () {
validateMobile(this);
});
// Handle form submission validation for feedback form
$("#<%= SubmitFeeId.ClientID %>").click(function () {
return validateFeedbackForm();
});
});
What’s happening here?
$(document).ready(function())ensures that the validation runs when the DOM is fully loaded.The
changeevent is used to trigger the validation functions (validateEmailandvalidateMobile) whenever the user changes the content of the email or mobile input fields.The
clickevent is used to handle form submission validation before actually submitting the form.
Step 2: Mobile Number Validation
function validateMobile(input) {
var value = input.value.trim();
$(input).removeClass("abc1");
// Skip validation if field is empty
if (value === "") return true;
// Must be exactly 10 digits
if (!/^\d{10}$/.test(value)) {
alert("Mobile number must be exactly 10 digits.");
$(input).addClass("abc1");
return false;
}
// Must start with 7, 8, or 9
if (!/^[7-9]/.test(value)) {
alert("Invalid mobile number. It should start with a digit from 7 to 9.");
$(input).addClass("abc1");
return false;
}
// All digits the same are not allowed
if (/^(\d)\1{9}$/.test(value)) {
alert("Invalid mobile number. All digits cannot be the same.");
$(input).addClass("abc1");
return false;
}
// Block full ascending or descending sequence
if (value === "1234567890" || value === "0987654321") {
alert("Invalid mobile number. Sequential digits not allowed.");
$(input).addClass("abc1");
return false;
}
return true;
}

Join the conversation! Your thoughts help the community grow.