jQuery: Textbox Validation and the blur() Event

Posted by Jetlogs @ 12:06 am
Category: jQuery,Web Development

Here is another jQuery tutorial demonstrating how to perform text validations using jQuery’s blur() event.

Its a fact that client-side client validation is no alternative to server-side validation especially in web applications. However, that doesn’t mean that we should drop client-side validation altogether. The truth is that client-side validation enhances a web application. First, the client-side validations provide a more responsive interface for users. Secondly, for a very large enterprise-grade application, shifting some of the validations on the client as a first pass filter will reduce the load on the server and improve its performance.

Now let’s move on to the tutorial. First of all, I’m going to use a simple registration form as an example for validation. Here is the demo for this tutorial:

Username: Username must be at least 4 characters in length

Password: Password must be at least 6 characters in length

Here is how the form looks like in code view:

<b>Username:</b> <i>Username must be at least 4 characters in length</i>
<input type="text" id="txt_username" name="username">
<span id="username_warning" style="color:red"></span>
<b>Password:</b> <i>Password must be at least 6 characters in length</i>
<br><input type="password" id="txt_password" name="password">
<span id="password_warning" style="color:red"></span>

In the example above, we’ve created a span for where our error message will appear with a unique id so they can be selected later on. What is important in this form is that we assign an id for each of the fields we need to validate so we can bind it with its corresponding jQuery event. Since we want to validate the text boxes once a user has supplied an input, the events of choice here will be both the change() and blur() event. However, the change() event has problems when the user reloads the page. Even with an invalid input, the validation will not trigger unless the user has changed the value of the input itself.

Now to validate the length of the user input, we must find the length of the text fields using the jQuery selectors. Here is how to do it:

$(document).ready(function(){
	$("#txt_username").blur(function()
	{
		var username_length;

		username_length = $("#txt_username").val().length;
		$("#username_warning").empty();

		if (username_length < 4)
			$("#username_warning").append("Username is too short");
	});

	$("#txt_password").blur(function()
	{
		var password_length;

		password_length = $("#txt_password").val().length;
		$("#password_warning").empty();

		if (password_length < 6)
			$("#password_warning").append("Password is too short");
	});
});

If you want to view the tutorial, you can download it here below:
Textbox Validation and the blur() Event Demo Download


22 Comments »

22 Comments to “jQuery: Textbox Validation and the blur() Event”

  1. guru
    1

    what is the need of jquery library? what it contains?

  2. Sreekanth
    2

    Good…Excellent…Keep it up !

  3. Seb
    3

    You should never handle security client side. This is subject to hacking (ie. using firebug).

    Do it using the server code. Much more secure :)

  4. novisithereagain
    4

    this stupid page disables browser back button :(
    http://jetlogs.org/2007/09/14/jquery-textbox-validation-and-the-blur-event/

    So I leave, never to return

  5. Andrew Johns
    5

    Seb, you miss the point of client side validation. It’s use is to filter out common mistakes to reduce the amount of page post backs, and reduce the amount of potential processing that the server has to do. However, as there is a large number of people who can turn off javascript, you should always recreate the same validation on the server.

    As far as I’m aware, most articles that discuss client side validation don’t advocate it’s use as a REPLACEMENT for server side security, but as a way of speeding up form filling for the majority of users who have javascript enabled, and will benefit from having their mistakes highlighted without having to wait for the page to be reloaded to find out that they missed a field.

  6. J-Fizzle
    6

    As a note to this… html 5 says all will have javascript…
    so, do the validation once it hits the server as a secondary measure and use the slickness that is JQuery to keep from posting back for no reason.

  7. tttt
    7

    egrgrghrgrgrgfrgrgfrgrgrgrg

  8. Bibn
    8

    This is a good article. Client side scripting is for the performance of the site. If we use jquery clients get the feedback immediatly

    http://techdotnets.blogspot.com

  9. rod
    9

    good article… Client validation first, server side validation next. Just dont’ forget to use server side validation too. cheers

  10. honar jems
    10

    in place of text can i use image tag
    if its possible plz give one example

  11. Anand
    11

    this one is the good example of validation.
    can we add more like showing error messages at the end of text box for each time without creating a special div or span in html?

  12. Abhishek mithu
    12

    uhihi

  13. Adriana
    13

    Very good this … is simple to implement also.

    Thanks

  14. asasa
    14

    sasdasdasdsdas

  15. jimjo
    15

    how if i put ‘space space space space’

    dang :D

  16. CSharp
    16

    ss

  17. ChintooKhaade
    17

    Hello Everyone,
    In JQuery blur method trigger blur event when element will lose their focus. In this article I am using blur () method on html page. In html page I had created some text boxes, if any textboxes got focus then back color of textboxes will white and if textboxes lost their focus then textboxes back color will change……………. for more details check out the following link…
    http://mindstick.com/Articles/db2a3600-a3a2-402d-86d1-0c290e451efa/?JQuery%20blur%20method

    Thanks !!!!!

  18. rr
    18

    hh

  19. Batista
    19

    You will get Batista Bomb if you will give spaces in the textboxes.

  20. Praveen Kumar Singh Bisen
    20

    Thanks very useful for beigner’s

  21. Ghanshyam
    21

    server side scriping lang more secure login

    if(empty($txtuname) || empty($txtpassword))
    {
    if(empty($txtuname))
    {
    $msg=”Enter username”;
    }
    if(empty($txtpassword))
    {
    $msg1=”Enter password”;
    }
    header(“Location:../index.php?uname=$msg&pass1=$msg1″);
    exit();
    }

  22. 756765756
    22

    vcbcbcvb

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License