Michal Kozak

I'm web developer & designer, quite the JavaScript nut, pure breed. I'm literally inhaling books and have unstoppable and never-ending need of learning. You can follow me on Twitter if you want or check out what I'm working on on Dribbble.

38 responses to “Building Real-Time Form Validation Using jQuery”

  1. Benny

    Hi,

    very nice addon. I have a question. I use your validation and the addon “http://jqueryui.com/demos/datepicker/”

    When I not filled in the date input field and submit the form I got the message “Please fill in your date”. Very nice. But when I click the date input field choose a date I have a javascript where focus the next field

    $(document).ready(function() {
    $(“#datepicker”).datepicker({
    onSelect: function(dateText, inst) {
    document.forms[0].ort.focus()
    }
    });
    });

    but the message “Please fill in your date” don´t disappear. What can I do, does the field disappear.
    When you need more information tell me.

    Thanks for your help.

    Regards
    Benny

  2. seyon

    Hi,
    I have a problem. This script is not working from my server at all. I couldn’t figure what went wrong. I have put all the files in the follwing directory without any modification. You can see that it is going to target page without doing any validation.
    http://www.nilavoip.com/forms/form.html
    Any help would be much appreciated.
    Thanks.
    -Seyon.

  3. Abhishek

    Maybe you could have provided a link to download the finished files!

  4. 27 New And Fresh jQuery Techniques

    [...] Real-Time Form Validation – jQuery In this tutorial by Michal Kozak you will learn how to build a useful client side and real time [...]

  5. Velizar

    I am very new to jquery. This article is excellent and it helped me in more ways than I expected as I’ve been pondering over a problem of mine for a day or so. For a .NET developer like me stuck with a single form on a page this method allows the flexibility to validate certain fields and ignore others until you need to validate them. I had a problem with a page which updated a couple of db fields but jquery validate would look at the entire form even though the fields were initially hidden. Your way allowed me to assign validation to the button click event of the dialog ( called from an .aspx page, using a master page, an update panel, and the button is inside a grid) and prevent a postback if validation is invalid. So thank you for this :)

    I don’t want to litter your blog so anyone with the same problem – happy to share the code, let me know dimitrov.velizar@gmail.com

  6. Theo

    Great tutorial, very well written. Thanks !
    .-= Theo´s last blog ..Muss eine Webseite in jedem Browser genau gleich aussehen? =-.

  7. Sandeep Malik

    good sir, its too help full.

  8. The Best Community Snippets On The Web Design Billboard in May'10 … | FlipTrends-Following the Hottest Google Trends

    [...] Article Link [...]

  9. jhon

    Is there anyway to validate dynamic generated element?

  10. Astrid

    Great tutorial!
    Everything is working fine with me until I use php to send the contact form to an email adress. Could somebody help me?
    My form is send to:

    The form.php sends the form to an email adress. But something goes wrong in combination with the validation of jquery.
    I know it has got something to do with:
    $(‘#send’).click(function (){
    var obj = $.browser.webkit ? $(‘body’) : $(‘html’);
    obj.animate({ scrollTop: $(‘#jform’).offset().top }, 750, function (){
    jVal.errors = false;
    jVal.fullName();
    jVal.birthDate();
    jVal.gender();
    jVal.vehicle();
    jVal.email();
    jVal.about();
    jVal.sendIt();
    });
    return false;
    });

    If I remove ‘return false’, the form is send to the email adress but of course the validation does not work the way it has to do.
    I am kind of a newbie at Javascript so I cant figure it out…

  11. KLM

    How would the function look without the scroll object?

  12. Weblapkészítés

    Hi Kozak!

    Yess, this post is a great work, I understood it from zero javascript knowledge!

    Best regards!

  13. Gian

    Hey, nice this plugin, but I think that is a little bug with date validation.

    I tried 22-13-2020 and I got OK with month 13

    hug

  14. masaru edo

    I like this tutorial. Great… banget lah :)
    .-= masaru edo´s last blog ..Tugas 2 Basis data =-.

  15. Notable Tech Posts – 2010.05.09 | The Life of Lew Ayotte

    [...] Building real time form validation using jQuery [...]

  16. bangbanggirl

    magnifique and so easy to use for the newbee i am ! thanks for sharing !

    bbg.

    1. Michal Kozak

      Thanks a lot, I’m glad you liked it :).
      .-= Michal Kozak´s last blog ..michalkozak: Just pre-ordered "Secrets of the JavaScript Ninja" (http://amzn.to/8ZOjqZ) by @jeresig, happy now. =-.

  17. KLM

    Is there a way to set up the system to validate if the id exists only, if the id does not exist skip the validation function for that id? My form is dynamic and on the first init of it it shows all fields with all validation id’s, on the second load, some id’s are removed to prevent the user from changing their entries and does not require the validation.

    if id exists – call validation function, if id is null skip validation function

    Great script, so close to getting it perfect for what I need.

    Thanks Michal!

    1. Michal Kozak

      Yeah, but you need something additional.

      Since you first have to GRAB that particular element/field to check if you should validate it or not.

      So basically you need to have that ID in case to easily grab this element, but you can still achieve what you want.

      Leave ID alone, but you can add a CLASS to that field, it can be:

      class=”validate”

      And then you could do something like:

      if ($(‘#yourElement’).hasClass(‘validate’)) {
      // validation script goes here
      }

      It can be done in many different ways – thing is, you have to plan it before you start building it. I didn’t need something like you do, so I didn’t optimize script for that.

      But you can use the method above, it will work just fine. You just have to figure something out with your IDs. For example: why do you remove them? Instead of removing the field, you can leave them and make them:

      disabled=”disabled”

      That way user won’t get to change it anymore, but the filed will still be there. Let me know how it worked out, cheers.
      .-= Michal Kozak´s last blog ..michalkozak: @amberweinberg I mean, it should be just readable and functional, that’s it. It does not have to look neither the same nor great. =-.

      1. KLM

        Michal, this is working really well. I am stuck on one thing, I am trying to incorporate the following CC luhn javascript and can’t seem to get it to work. How do I check the the value is = 0; i -= 2) {
        sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cc.charAt (i), 10)];
        }
        for (i = cc.length – 1; i >= 0; i -= 2) {
        sum += parseInt (cc.charAt (i), 10);
        }
        return (sum % 10) == 0;
        }

  18. links for 2010-05-04 « Mandarine

    [...] Building Real-Time Form Validation Using jQuery (tags: jquery form tutorial) [...]

  19. rezyde

    very very nice! I was looking for a form to use that is simple. Thanks

    1. Michal Kozak
  20. KLM

    If I set a form field to display:none how do I get the validation to ignore those fields?

  21. Ryan Turki

    Great tut Michal ! Thanks

  22. joddy street

    this is so far the best tute i have read about jquery form validation. loved the effect

  23. Abdullah Al Mamun

    WOW!!
    Simply Awesome……. thanks a lot……….very useful.
    .-= Abdullah Al Mamun´s last blog ..mamunabms: @adam_griffiths Could you please build a library for MoneyBookers Payment Gateway. #codeigniter =-.

  24. Jared Detroit

    This is great! I’ve been telling myself I need to learn more jquery and this is a perfect example of not only what I need for my website but how to actually do it. Thanks for the complete example!

  25. Keiron Lowe

    Fantastic Tutorial, I love this website :D
    I’m assuming this doesnt send the email, this is just the validation?

  26. Carli

    Well the date validation is a bit bugy, date format is like dd-mm-yyyy, and if I enter 15-15-2010, the date is valid, but we all know that there is only 12 months in one year. ;) In LIVE example …

Leave a Reply