1

Actually I am new to jquery as well as web designing

Now I need some piece of code which will invoke after blur event on a text box.

I need jquery code for validating a name field in the html form.

1
  • @ZoltanToth i mean check weather that is a name or not...for example if i enter 3sam ,it should show error Commented Jul 4, 2012 at 17:42

5 Answers 5

5

Try this - http://jsfiddle.net/gZNkt/

$("#fname").on("blur", function() {
    if ( $(this).val().match('^[a-zA-Z]{3,16}$') ) {
        alert( "Valid name" );
    } else {
        alert("That's not a name");
    }
});

It will check your name to be 3-16 characters long and contain only letters, so sam will validate, but 3sam - won't

Sign up to request clarification or add additional context in comments.

1 Comment

2-16 characters are better, there are names from asian countries like "Li" which are shorter then 3 characters
2

Make sure you add a latest version of jQuery.... Here is a sample for validating for not null.

    $(document).ready(function() {
        $('input#fname').on('blur', function() {
            if( $(this).val() == ''  || $(this).val() == "null") {
                // Your code to handle error
            } else {
                            return true;
                    }
        })
    });

1 Comment

this will fail if the user enters 32#78@*! or 3sam as a name
1
    $("#reg").bind("keypress", function (event) {
            if (event.charCode!=0) {
                var regex = new RegExp("^[a-zA-Z]+$");
                var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
                if (!regex.test(key)) {
                    event.preventDefault();
                    return false;
                }
            }
        });

you disable press key and allow only charecters ....

Comments

1

I think you want to validate your text box after focus out or blur in jQuery.

You can use below code for the same purpose :-

$(document).ready(function()
{
   $("#fnam").blur(function() {

      if($(this).val() == null || $(this).val() == undefined || $(this).val() == "")
      {
         alert("field is empty");
      }

   });

// if you want this validation on focus lost
 $("#fnam").focusout(function() {

  if($(this).val() == null || $(this).val() == undefined || $(this).val() == "")
  {
     alert("field is empty");
  }

   });

});

Comments

0

Why dont you try with the jQuery Validate plugin

3 Comments

actually iam new to jquery...can you help me pls
First of all you gotta download the Plugin and add it to your html code, you can download the necessary files in jquery.bassistance.de/validate/jquery-validation-1.9.0.zip . Then when you create your form, for example the input where the name it's gonna go you create it like this:<br/> <input id="cname" name="name" size="25" class="required" minlength="2" /> And then in you javascript you just do:<br/> $('#yourForm').validate();
In the very first link i provided there is a whole lot of other examples you can experiment with

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.