user3668910
user3668910

Reputation: 11

jquery form validation without click -> when ok show div

is it possible to do this automatically. mean when i type text and click on the second textfield autocheck the first one. then when both ok show the div2 and so on.

here is some code

var step1 = function() {
    var first = $("#f_name").val();
    var last = $("#l_name").val();
    var error = false;
    if (first == "") {
        $("#f_name").next().text("*ErrorMsg");
        error = true;
    } else {
        $("#f_name").next().text("");
    }
    if (last == "") {
        $("#l_name").next().text("*ErrorMsg");
        error = true;
    } else {
        $("#l_name").next().text("");
    }
    if (error == false) {
        $("#send").submit();
        $('#div1').show('slow');
    } else {
        returnfalse;
    }
}
var step2 = function() {
    var email1 = $("#e_mail").val();
    var adress1 = $("#adress").val();
    var error2 = false;
    if (email1 == "") {
        $("#e_mail").next().text("*ErrorMsg");
        error2 = true;
    } else {
        $("#e_mail").next().text("");
    }
    if (adress1 == "") {
        $("#adress").next().text("*ErrorMsg");
        error2 = true;
    } else {
        $("#adress").next().text("");
    }
    if (error2 == false) {
        $("#send2").submit();
        $('#div2').show('slow');
    } else {
        returnfalse;
    }
}
$(document).ready(function() {
    $('#div1').hide();
    $('#div2').hide();
    $("#send").click(step1);
    $("#send2").click(step2);
});

hope anyone can help me. and sorry for my bad english :)

greatings

Upvotes: 1

Views: 78

Answers (1)

Steve
Steve

Reputation: 569

The way that I would do it is:

  1. Assign a variable, something like numSteps and set its initial value to 1
  2. onFocus and onBlur, run a function that steps through each field, based on numSteps
  3. If any fields are empty (or however you want to validate them), set error = true
  4. if !error numSteps++
  5. Make all elements up to numSteps visible

Hope this helps

Very crude example, but demonstrates what I was referring to: http://jsfiddle.net/aSRaN/

Upvotes: 2

Related Questions