Help adding custom validators to the jquery.validator-0.3.4.min.js

Mar 14, 2011 at 6:14 PM
Edited Mar 15, 2011 at 10:50 AM

Hi,

 

Thank you for providing the validator, it works great. I was wondering if you can help me write a custom validator that checks a textbox value and returns

the value if it is not in the range which then should causes the validation to show like the require.

 

the value i require is greater than 7,500 and less than 100,000

 

Any help will be greatly appreciated

thank

shy9010

Coordinator
Mar 16, 2011 at 10:30 AM
Edited Mar 16, 2011 at 11:34 AM

Sure:

<input type="text" validate="form" custom="Invalid Value. Please ensure your value is between 7,500 and 100, 000." customFn="var val = parseInt(this.value); return val > 7500 && val < 100000;" />

If you did want to add the custom valdiator to jquery.validator-0.3.4.min.js (useful for having it defined in only one location for you to change when you plan to use it in multiple places) it would like this:

var validators = {
	"email": function() { return ...; },
	"validnum" : function() { var val = parseInt(this.value); return val > 7500 && val < 100000; }
};
<input type="text" validate="form" validnum="Invalid Value. Please ensure your value is between 7,500 and 100, 000." />

Mar 16, 2011 at 12:42 PM
Edited Mar 16, 2011 at 12:55 PM

Hi mcm,

Thank you very much for your help, it seems alot more clearer to me now....i have one more question if thats ok, i have comma's in my interger so i need to strip those out before the parseint

Code:

    "validnum": function () {
        var noComaVal = (this.value).replace(/,/g, '');
        var val = parseInt(noComaVal);
        return val >= 7500 && val <= 100000;
    }

do i use (this.value) to read the value from my  <asp:TextBox> string

The above code doesnt seem to work,

here is my asp:text box control

<asp:TextBox ID="txtBAmountCover" Validate="form" 
validnum
="<p><b>Invalid Value.</b><br/> Please ensure your value is between <br/>7,500 and 100, 000.</p>"
require
="<p><b>Required Field Missing</b><br/>Please enter your desired Loan Amount.</p>">
</
asp:TextBox>

Thank again in advance, i am c# developer and new to jquery

regards
shy9010

 

Coordinator
Mar 17, 2011 at 12:14 AM

My fault, I made customFn to be consistent with other javascript events like onclick where "this" refers to the input element the attribute is defined on (so indeed "this.value" should be the value from the asp textbox) and made the function to return true if valid or false if not. But for some reason I did the opposite for the personally defined validator collection and instead made "this" the input value (in javascript you can change what "this" refers to when calling a function) and made false the value to return if the value is valid. This has been tidied up and should now work with your sample code as it now stands:

http://validator.codeplex.com/releases/view/62692