Question

I validated some fields in my form.. But i have some issues..If without enter fields it shows error message.. If fill out the field still error message is showing..

How to put that ?

My code

 $("#Name").focus();
$("#Name").blur(function(){
    var name=$('#Name').val();
    if(name.length == 0){
        $('#Name').after('<div class="red">Name is Required</div>');
    }
    else {
    return true;
    }
});

   $("#Address").blur(function(){
    var address=$('#Address').val();
    if(address.length == 0){
        $('#Address').after('<div class="red">Address is Required</div>');
        return false;
    }
    else {
    return true;
    }
});

can anyone help me please?????

Was it helpful?

Solution

You should remove this labels after that user input some data

$("#Name").focus();
$("#Name").blur(function(){
    var name=$('#Name').val();
    if(name.length == 0){
        $('#Name').after('<div class="red">Name is Required</div>');
    }
    else {
        $('#Name').next(".red").remove(); // *** this line have been added ***
        return true;
    }
});

   $("#Address").blur(function(){
    var address=$('#Address').val();
    if(address.length == 0){
        $('#Address').after('<div class="red">Address is Required</div>');
        return false;
    }
    else {
        $('#Address').next(".red").remove(); // *** this line have been added ***
        return true;
    }
});

jsfiddle: DEMO

OTHER TIPS

Your code has bug that it places div as many as time as you blur in empty textbox. This bug is also removed by my code See-:

Working Demo http://jsfiddle.net/XqXNT/

$(document).ready(function () {
    $("#Name").focus();
    $("#Name").blur(function () {
        var name = $('#Name').val();
        if (name.length == 0) {
            $('#Name').next('div.red').remove();
            $('#Name').after('<div class="red">Name is Required</div>');
        } else {
            $(this).next('div.red').remove();
            return true;
        }
    });

    $("#Address").blur(function () {
        var address = $('#Address').val();
        if (address.length == 0) {
            $('#Address').next('div.red').remove();
            $('#Address').after('<div class="red">Address is Required</div>');
            return false;
        } else {
            $('#Address').next('div.red').remove();
            return true;
        }
    });

});

It's better if you use required attribute which does the same work with less code and better manner.

HTML5

<input type="text" name="name" required />
<input type="text" name="address" required />

Try this code (I just changed the structure and added return false) :

$("#Name").focus()

$("#Name, #Address").blur(function(){
    if($(this).val().length == 0){
        $(this).after('<div class="red">This field is required</div>');
    } else {
        $(this).next('.red').remove()
    }
});

But I think the best way is to add the required attribute to your fields like this :

<input type="text" name="name" required />
<input type="text" name="address" required />

Try to remove the added html in else condition.

 if(name.length == 0){
        $('#Name').after('<div class="red">Name is Required</div>');
    }
    else {
    $('.red').empty(); //here
    return true;
    }

And the same for $("#Address")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top