Wednesday, November 18, 2009

JQuery Validation

Just wanna share some JQuery validation sample code which might be useful:



//if you put CssClass="required" on the dropdownlist,


//then JQuery's default required field validator will valiate the dropdownlist,


//but it only returns false if there is no items in the dropdownlist


// -------------------Custom function: Required Validator for Drop Down List----------------------------------------------------


jQuery.validator.addMethod("Validate_ddls", function(value, element) {



if (null != element) {


var iValue = new Number(element[element.selectedIndex].value);


//if ddlColumns is not populated, selectedIndex will be -1


if (iValue <= 0 iValue == NaN) {


return false;


}


else {


return true;


}


}


else {


return false;


}


}, jQuery.validator.messages.required);




// -------------------Custom function: Positive_Integer_Exclude_Zero----------------------------------------------------


jQuery.validator.addMethod("Positive_Integer_Exclude_Zero", function(value, element) {


return this.optional(element) /^[1-9]\d*$/.test(value);


}, "Positive Integer Only");



// -------------------Custom function: Positive_Integer----------------------------------------------------


jQuery.validator.addMethod("Positive_Integer", function(value, element) {


return this.optional(element) /^[0-9]\d*$/.test(value);


}, "Positive Integer Only");



// -------------------Custom function: Positive_Decimal_Exclude_Zero----------------------------------------------------


jQuery.validator.addMethod("Positive_Decimal_Exclude_Zero", function(value, element) {


return this.optional(element) /(^\d*\.?\d*[1-9]+\d*$)(^[1-9]+\d*\.\d*$)/.test(value);


}, "Invalid Amount");



// -------------------Custom function: Positive_Decimal----------------------------------------------------


jQuery.validator.addMethod("Positive_Decimal", function(value, element) {


return this.optional(element) /(^\d*\.?\d*[0-9]+\d*$)(^[0-9]+\d*\.\d*$)/.test(value);


}, "Invalid Amount");



// -------------------Custom function: Validate_Email----------------------------------------------------


jQuery.validator.addMethod("Validate_Email", function(value, element) {


return this.optional(element) /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(value);


}, "Invalid Email address");







// -------------------Validate the whole Form----------------------------------------------------


$(document).ready(function() {


$("#aspnetForm").validate({


rules: {


<%=txtTag.UniqueID %>:{


required: true


},


<%=ddlTables.UniqueID %>:{


Validate_ddls: true


},


<%=ddlColumns.UniqueID %>:{


Validate_ddls: true


}


},


messages: {


<%=txtTag.UniqueID %>:{


required: "Tag is required"


},


<%=ddlColumns.UniqueID %>:{


required: "Field is required"


}


}


});


});






<script type="text/javascript" src="../include/APF_js/jquery-1.3.2.js"></script>


<script type="text/javascript" src="../include/APF_js/jquery.validate.js"></script>




blog comments powered by Disqus