Feedback

About Designerfoo and Me!

I am Manoj Sachwani, a webmaster, wannabe illustrator, designer, developer, amongst other things. I have over 14 years of experience, developing, designing and deploying websites. I am geeky, hairy and and I love what I do.

I stumbled & bought this domain - Designerfoo.com - because I thought it would be a "cool" domain to have, as time went on, I realized that I want to become - "Designerfoo" - and grow my knowledge in design, illustration, web design and web development.

I am webmaster, I design and develop websites with an edge. How may I help you?
Contact   -   @designerfoo   -   Facebook
Who is

jQuery validation plugin: Rules

DesignerfooForms + jQuery • 09-11
Comment(s)

I was working on a project and wanted to validate a form. Since I was already using and including jQuery, I thought I did validate the form using jQuery itself.

I had heard and read about the validation plugin by Jörn Zaefferer and I decided to use it. Sure enough its easy to use and for a basic form, its mind blowingly “simple”.

But, yes there is a but, but on the good side..

But, being a control freak, I wanted more control in form of validating the form, my way. I did some looking around and stumbled on to some posts, docs, etc. and sure enough I could add how the plugin validate each field in the form.

But, I still wanted to make it easier to validate, not having to add functions to the validator and again having to add those functions as rules to input controls, I thought I did use regex – an ol’ friend, to help me.

Below is what I used and it’s pretty simple.


$.validator.addMethod(
"custom_rules",
function(value, element, reg) {
if (reg.constructor != RegExp)
reg = new RegExp(reg);
else if (reg.global)
reg.lastIndex = 0;
return this.optional(element) || reg.test(value);
},
"Wrong input found, please check your input."
);

Adding this one method to the validator, I was able to add as many rules as I wanted without having to add more methods or functions to the validator class. For e.g.

For the text input control accepting phone numbers in the format xxx-xxx-xxxx I used the following rule.


$("#applicantPhone").rules("add", { custom_rules: /[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/ });

  Demo for jQuery Validation Rules Regex

Simple isn’t it? Offcourse, I have used much simpler regex in the form :)

Post Rating 3.00 out of 5


Subscrive via feedburner rss

Subscrive via feedburner email

Please Note:

As mentioned on the Validations Plugins documentation for the addMethod, it is cleaner to wrap regex in their own functions per ".selector" or input objects. For more on regex and more on this note.

Linkbacks and Thanks!

I go the idea for this from stackoverflow.com. Thanks to those guys there.
8 Responses to “jQuery validation plugin: Rules”
Comment History Slider
    March 10, 2011 at 8:22 am
    Author: Ravinder

    Author: RAVINDER

    plzz help me..can we use if condition in jquery validation like:

    $().ready(function()
    {

    $(”#signupForm”).validate(
    {
    rules: {
    element_19:
    { required: true,
    if($(’#event’).val()==’8′)
    { other: “required”
    }
    else{ //no }
    ……………………
    plzz help me..

    December 1, 2009 at 2:24 pm
    Author: Mr52

    umm it aint working please check once the demo url

      December 1, 2009 at 2:26 pm
      Author: Designerfoo

      Sent you an email with screenshots. Do you have a url online where I can check whats up?

    December 1, 2009 at 2:09 pm
    Author: Mr52

    awesome thanks :D

      December 1, 2009 at 2:12 pm
      Author: Designerfoo

      pleasure :)

    December 1, 2009 at 1:16 pm
    Author: Mr52

    umm any Demo for this please..

    i would really need a demo for this i have some problems

      December 1, 2009 at 1:49 pm
      Author: Designerfoo

      Hey MR52,

      added a demo link and mailed you the same too, thanks!



Leave a reply :)

Name:

(required)

Mail (will not be published):

(required)

Website:

Comment:


Subscribe

Links

Eventify – Simple Events

NextGEN Resize Wordpress Plugin

Categories

Recent Posts

Looking for something?

Thanks to Subscribers

Feedburner : 53


Twitter : 3017

Thank you for taking the time out.

Your Email (required)

Your Email (required)

Your Feedback

Captcha
captcha

What to do you think of Eventify - Simple Events?

View Results

Loading ... Loading ...

What do you think about the NextGEN Resize Plugin?

View Results

Loading ... Loading ...

What do you think about the site design/UI/IA?

View Results

Loading ... Loading ...

  X Close Feedback.