|
|
Initializing the Library:
To initialize the validateJS library, add the references to jQuery and validateJS scripts, and then create a ValidationManager object in your page load event. Most of the methods can be chained to save typing more code. |
var vManager = new ValidationManager()
.addValidator(["amount"], [{ type: ValidationType.Required, message: "Amount is required." }])
.initialize();
Always remember to initialize your validation manager object.
|
|
Setting up the markup:
Some specific attributes are need to be added to the input fields, apart from that we need to add <div> tag to show the validation notifications. Here is how this is done: Input element
<input id ="txtAmount" maxlength="20" validatorName ="amount"/>
Display validator(this has to be near to the target input element)
<div validator = "amount"></div>
|
|
Required Field Validation
Syntax:
vManager.addValidator([validatorName], [{ type: ValidationType.Required, message: messageText }])
.initialize();
Usage:
vManager.addValidator(["name"], [{ type: ValidationType.Required, message: "Please enter your name." }])
.initialize();
Demo:Rule : A value is required. Please Enter Your Name: |
|
Compare Validation
Syntax:
vManager.addValidator([validatorName]
, [{ type: ValidationType.Compare
, rule: { compareType: compareType, value: value }, message: messageText }])
.initialize();
Usage:
vManager.addValidator(["salary"]
, [{ type: ValidationType.Compare
, rule: { compareType: CompareType.LessThanEqual, value: 100 }
, message: "Salary should be less than or equal to $100." }])
.initialize();
Demo:Rule : Value must be less than or equal to 100. Enter the Salary You Want: |
|
Range Validation
Syntax:
vManager.addValidator([validatorName]
, [{ type: ValidationType.Range, rule: [minValue, maxValue]
, message: messageText }])
.initialize();
Usage:
vManager.addValidator(["salary"]
, [type: ValidationType.Range
, rule: [1000, 2000]
, message: "Salary should be between $1000 and $2000." }])
.initialize();
Demo:Rule : Value must be between 1000 and 2000. Enter the Salary You Want: |
|
Regular Expression Validation
Syntax:
vManager.addValidator([validatorName]
, [{ type: ValidationType.RegularExpression, rule: regularExpression
, message: messageText }])
.initialize();
Usage:
var emailRegExp = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$";
vManager.addValidator(["email"]
, [type: ValidationType.RegularExpression
, rule: emailRegExp
, message: "The email id that you entered does not seem to be correct." }])
.initialize();
Demo:Rule : Text entered should be an email id in correct format. Enter Your Email Id: |
|
Custom Validation
Syntax:
vManager.addValidator([validatorName]
, [{ type: ValidationType.Custom, rule: customValidationFunctionName
, message: messageText }])
.initialize();
Usage:
vManager.addValidator(["salary"]
, [type: ValidationType.Custom
, rule: CustomSalaryValidation
, message: "Salary should be in multiples of 100." }])
.initialize();
Demo:Rule : Value should be a multiple of 100. Enter the Salary You Want: |
|
Multiple Validations
Syntax:
vManager.addValidator([validatorName]
, [{ type: ValidationType.Custom, rule: customValidationFunctionName
, message: messageText }, ...])
.initialize();
Usage:
vManager.addValidator(["salary"], [{ type: ValidationType.Required
, message: "Salary cannot be blank." },
{ type: ValidationType.Range
, rule: [1000, 2000]
, message: "Salary should be between $1000 and $2000." },
{ type: ValidationType.Custom
, rule: CustomSalaryValidation
, message: "Salary should be in multiples of 100." }])
.initialize();
Demo:Rules: Value is required. Value must be between 1000 and 2000 Value should be a multiple of 100. Enter the Salary You Want: |
|
Validation Firing On Change Event
Syntax:
vManager.addValidator([validatorName]
, [{ type: ValidationType.Custom, rule: customValidationFunctionName
, message: messageText }, ...])
.validateOnTextChange(true, SetSummary)
.initialize();
Usage:
vManager.addValidator(["salary"], [{ type: ValidationType.Required
, message: "Salary cannot be blank." },
{ type: ValidationType.Range
, rule: [1000, 2000], message: "Salary should be between $1000 and $2000." },
{ type: ValidationType.Custom
, rule: CustomSalaryValidation, message: "Salary should be in multiples of 100." },
{ type: ValidationType.MaxLength, rule: [4], message: "Maximum length for this field is 4." },
{ type: ValidationType.MinLength, rule: [2], message: "Minimum length for this field is 2." },
{ type: ValidationType.Numeric, message: "Only numbers are allowed in this field"}])
.validateOnTextChange(true, SetSummary)
.initialize();
Demo:Change the value and click outside the field to fire the validation. Enter the Salary You Want: |
|
.validate(validatorList)
Validates the validators which are provided in the array as a parameter.
|
|
.validateAll()
Validates all the validators that are currently added. |
|
.addValidator(validators, params)
Adds a new validator.
Validation Types
|
|
.getValidationResults(validator)
Returns the validation results in the form of an object array. Return value format: {validatorName, sourceControl, type, message}
|
|
.setPassImage(boolean)
Set a custom image to show if the validation is passed.
|
|
.setFailImage(boolean)
Set a custom image to show if the validation is failed.
|
|
.showFailToolTips(boolean)
Show/hide validation message's tooltips if the validation is failed.
|
|
.showFailImgNotification(boolean)
Show/hide image notification if the validation is failed.
|
|
.showPassImgNotification(boolean)
Show/hide image notification if the validation is passed.
|
|
.highlightBackground(boolean)
Highlight background of he input element if the validation is failed.
|
|
.validateOnTextChange(f, onComplete)
Fires validation for the validator on text change event.
|
|
.isValid(validatorList)
Checks if any validator is correctly validated or not.
|