Hello,
I am working in c# asp.net project. I have 4 to 5 pages.
I am using RequiredFiledValidator, RegilarExpressionValidator....etc on all pages.
But I want to use Jquery validation with common .js file with all validation in it and want to access it on all pages. So i can avoid code duplication on all pages.
Please suggest some example.
Also, can share Jquery or javascript all type of validation on page .
Thanks in advance!

Abhishek YadavRank #217 · 11.3K reputationPosted Oct 10, 2025, 3:13 AM EDT
Step 1: Add jQuery Validation Library
Step 2: Create
common-validation.js$(document).ready(function () { // Common function to attach validation on any form function applyValidation(formSelector) { $(formSelector).validate({ errorClass: "text-danger", highlight: function (element) { $(element).addClass("is-invalid"); }, unhighlight: function (element) { $(element).removeClass("is-invalid"); }, rules: { txtName: { required: true, minlength: 3 }, txtEmail: { required: true, email: true }, txtPassword: { required: true, minlength: 6 }, txtConfirmPassword: { required: true, equalTo: "#txtPassword" }, txtPhone: { required: true, digits: true, minlength: 10, maxlength: 10 }, txtAge: { required: true, number: true, min: 18, max: 100 } }, messages: { txtName: { required: "Please enter your name", minlength: "Name must be at least 3 characters long" }, txtEmail: { required: "Please enter your email", email: "Please enter a valid email" }, txtPassword: { required: "Please enter password", minlength: "Password must be at least 6 characters" }, txtConfirmPassword: { required: "Please confirm password", equalTo: "Passwords do not match" }, txtPhone: { required: "Please enter phone number", digits: "Only digits allowed", minlength: "Phone must be 10 digits", maxlength: "Phone must be 10 digits" }, txtAge: { required: "Please enter your age", number: "Please enter a valid number", min: "Age must be at least 18", max: "Age cannot exceed 100" } } }); } // Apply validation automatically to all forms with class 'validated-form' $("form.validated-form").each(function () { applyValidation(this); }); });Step 3: Use It on Any Page
Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 13, 2025, 2:44 AM EDT
How to Use Common jQuery Validation in All ASP.NET Pages
If you’re using multiple ASP.NET Web Forms pages (4–5 or more) and currently using
RequiredFieldValidator,RegularExpressionValidator, etc., you can replace those with a single common jQuery validation file to avoid repeating the same rules on every page.Step-by-Step Solution
1. Add jQuery and jQuery Validation plugin
Download or include these scripts in your Master Page (or each page if not using Master Page):
2. Create a common validation file (e.g.,
site-validation.js)Place it in your
Scriptsorjsfolder.// site-validation.js $(document).ready(function () { $("form").each(function () { $(this).validate({ errorClass: "errorText", rules: { txtName: { required: true, minlength: 3 }, txtEmail: { required: true, email: true }, txtPassword: { required: true, minlength: 6 }, txtConfirmPassword: { required: true, equalTo: "#txtPassword" }, txtMobile: { required: true, digits: true, minlength: 10, maxlength: 10 } }, messages: { txtName: { required: "Please enter your name", minlength: "Name must be at least 3 characters" }, txtEmail: { required: "Please enter an email address", email: "Please enter a valid email" }, txtPassword: { required: "Please enter a password", minlength: "Password must be at least 6 characters" }, txtConfirmPassword: { required: "Please confirm your password", equalTo: "Passwords do not match" }, txtMobile: { required: "Please enter mobile number", digits: "Only digits allowed", minlength: "Enter 10-digit number", maxlength: "Enter 10-digit number" } }, errorPlacement: function (error, element) { error.insertAfter(element); } }); }); });3. Include this JS file on all pages
In your Master Page (recommended):
Now, every content page inside that master will automatically have jQuery validation enabled for matching control IDs.
4. Example ASPX Page
Because your control IDs are matched in the JavaScript rules (
txtName,txtEmail, etc.), validation works automatically across all pages.Advantages
Optional — Custom Common Validation Functions
You can even define reusable functions in your
site-validation.js:function validateEmail(email) { var pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return pattern.test(email); } function validateMobile(mobile) { return /^[0-9]{10}$/.test(mobile); }Then you can call these functions anywhere in your project.
Final Folder Structure Example
/Scripts +-- jquery.min.js +-- jquery.validate.min.js +-- site-validation.js /Pages +-- Register.aspx +-- Login.aspx +-- Contact.aspx /Site.MasterOutput Behavior
When the user clicks Submit, if any field is empty or invalid:
Red error messages appear under the inputs.
Form won’t submit until all validations pass.
In Summary
To use jQuery Validation in all ASP.NET pages:
Add jQuery + validation plugin in Master Page.
Create one
site-validation.jsfile with all your validation rules.Include it globally.
Keep all field IDs consistent across pages.
Sushant TorankarRank #3,026 · 476 reputationPosted Oct 14, 2025, 2:43 PM EDT
hello,
thanks for reply.
i tried, but it's not working, as i am using update panel i have only form tag in master page..