2

would like to show all errors on the right of a form when user makes input error. Example if user enters incorrect e-mail format. I have created the div but I am not able to correctly display the errors and remove them as soon as the user corrects the input. Till now I am just highlighting the textbox using the error id.

jquery:

    var form = $("#contact_form");
var name = $("#name");
var nameInfo = $("#nameInfo");
var email = $("#email");
var emailInfo = $("#emailInfo");
var message = $("#message");
var messageInfo = $("#messageInfo");
var note = $("#note");

name.blur(validateName);
email.blur(validateEmail);
message.blur(validateMessage);


form.submit(function(){
    if(validateName() & validateEmail() & validateMessage()){
        return true;
    }else{
        return false;
    }
});


function validateName(){
    if(name.val().length < 5){  
        name.addClass("error");
        return false;

    }else{

        name.removeClass("error");
        return true;

    }

}
1
  • Have you tried any form validator plugin? Commented Jan 2, 2013 at 10:29

2 Answers 2

2

i recommened you to use plugins rather that creating your own...

jquery validation plugin

all the things that you wanted in your form is already done ... easy to use and modify..

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

1 Comment

0

You could use like this..

function validateName(){
    if(name.val().length < 5){  
        $("#nameError").html("Name should contain atleast 5 characters");
        name.addClass("error");
        return false;

    }else{
        $("#nameError").html("");
        name.removeClass("error");
        return true;

    }

}

nameError is an ID of the DIV you have created to show the error..

On name field keyDown event, just clear the error msg like this..

$("#nameError").html("");

1 Comment

you gave me the idea that I can create a separate div for each error next to each textbox and fill it up or remove it as necessary.

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.