+ Reply to Thread
Results 1 to 10 of 10

Thread: jQuery: Validation

  1. #1
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    jQuery: Validation

    G'day everyone.

    Welcome to another one of my jQuery tutorials that many people seem to love. This is the fourth tutorial and I will be showing you how to use your
    FIRST jQuery plug-in, unless you have done it before. These plug-ins are really easy to find and use thanks to the dedicated people that made them
    this way. There is a plug-in for nearly anything you can think of, so I will be showing you how to validate your user inputs the easy way

    Let's get started. The name of this plug-in is "Validation", it is one of the oldest plug-ins for jQuery and has been proven to be the best validator.
    This should hopefully encourage you to get into using it and don't rely on Server side programming languages as much. I will be showing you how to
    make sure a user inputs some data, only enter numbers, valid email and a URL which will be optional.

    To use the plug-in you need to download the JS file, attached at the bottom of the post or visit their homepage, also at the bottom. Let's get started
    by creating the form, all explained below.
    Code:
    <form type="GET" action="" class="form" id="form">
        <
    p>
            <
    label for="name">Name:</label>
            <
    input type="text" id="name" name="name" size="30" class="required" minlength="2" maxlength="25" />
        </
    p>
        <
    p>
            <
    label for="rank">Rank:</label>
            <
    input type="text" id="rank" name="rank" size="30" class="required" minlength="4" maxlength="15" />
        </
    p>
        <
    p>
            <
    label for="postCount">Post Count:</label>
            <
    input type="text" id="postCount" name="postCount" size="6" class="required digits" maxlength="6" />
        </
    p>
        <
    p>
            <
    label for="postCount">Email:</label>
            <
    input type="text" id="email" name="email" size="30" class="required email" maxlength="30" />
        </
    p>
        <
    p>
            <
    label for="postCount">URL:</label>
            <
    input type="text" id="url" name="url" value="http://" size="30" class="url" maxlength="40" />
        </
    p>
        <
    p>
            <
    input class="submit" type="submit" value="Submit" />
        </
    p>
    </
    form
    You might not understand what all this means yet, nor will most of you ever found use for the label tag but here it is required. We create our
    normal form tags and add our type, the action can be added if you wish to do something with this information (this tutorial we will not) we must add
    a class and an id. Make sure they are the same.

    Code:
    <label for="name">Name:</label
    Now we have created our form, we must add labels so Validation knows what to look for. We must add the "for" attribute to the label which
    will be equal to the name and the id of the input field followed. Now we have created our label, we need create our input field.

    We need to just create a normal input field, but the name and id must be the same and they have to be the same as the label tag that was just before
    it. You can add anything else you wish to add to the input tag, but one thing you may not change is the class. The plug-in reads the class and that
    is how it knows what to validate. In our first example we only wish to make sure the user inputs something so you just have to add the class
    "required". But I don't want them to input more than 25 characters, so I just added;
    Code:
    maxlength="25" 
    Which is an atttribute of the input field for text. There are many other words you can add to validate it further. Here I have used;
    digits - Only numbers which is required
    email - A valid email which is required
    url - A valid url, must start with "http://" set by the plugi-in so I have used "value" to help them out. The URL is not required.

    There are many others which can be found here.

    If it is not validated correctly, it will show an error message next to the textbox. When you click on submit, if one is not valid it will show the
    message and not complete the action.


    More about the plug-in can be found here;
    Plugins/Validation - jQuery JavaScript Library

    Plug-in homepage (includes download);
    bassistance.de » jQuery plugin: Validation

    More plug-ins can be found here;
    Plugins - jQuery JavaScript Library

    Hope you enjoyed this tutorial. +rep if you liked

    Regards,
    Brandon
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: jQuery: Validation

    Nice tutorial, Brandon! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: jQuery: Validation

    Very cool... I could really use something like this in some of my projects.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: jQuery: Validation

    nice tutorial..really
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  6. #5
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,391
    Rep Power
    30

    Re: jQuery: Validation

    Really nice Tuto .

  7. #6
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: jQuery: Validation

    Thanks everyone. Winged, using this would be much easier than most other ways of validation. I will be using this from now on, also if you read more about the plug-in you can learn how to customize the output.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  8. #7
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: jQuery: Validation

    I have a few pretty OBNOXIOUS validation codes. If this is easier, I'm all for it.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #8
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: jQuery: Validation

    OK, can I see your code that you use now? I have John's validation class on my localhost somewhere.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  10. #9
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: jQuery: Validation

    Short version (since I can't hand the code out)...
    I have a dynamically generated list of items, each of which has a dynamically generated dropdown list. Based on the item on the dropdown, another field may or may not be mandatory before the form can be submitted.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: jQuery: Validation

    OK Not bad at all
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Jquery username validation not working
    By jlr2k8 in forum JavaScript and CSS
    Replies: 0
    Last Post: 06-24-2011, 11:39 AM
  2. Replies: 1
    Last Post: 04-22-2011, 11:18 AM
  3. Validation help
    By movax85 in forum General Programming
    Replies: 7
    Last Post: 03-23-2011, 05:49 AM
  4. php mail() validation
    By ststacytucker9 in forum PHP Development
    Replies: 5
    Last Post: 05-12-2010, 01:22 AM
  5. PHP Validation
    By Alex_j in forum PHP Development
    Replies: 3
    Last Post: 04-27-2010, 08:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts