Introduction
This will explain the concept of validation using the class name in Javascript and jQuery. You just need to add the class 'mandatory' in control, then this control will be required for submission of the form. If it is not filled, then it will be highlighted red.
Code:
- function Validation()
- {
- var isValid = true;
- var classname = 'mandatory';
- $('.' + classname + '').each(function (i, obj)
- {
- if (obj.value == '')
- {
- isValid = false;
- return isValid;
- }
- });
-
- if (!isValid)
- {
- $('.' + classname + '').each(function (i, obj)
- {
- if (obj.value == '')
- {
- obj.style.border = '1px solid red';
- }
- else
- {
- obj.style.border = '1px solid black';
- }
- });
- alert('Please fill mandatory details');
- }
- if (isValid)
- {
- return confirm('Are you sure you want to save information? Once information stored will not be updated.')
- }
- return isValid;
- }
For Example
ASP textbox :