views:

381

answers:

3

Hi there,

I have a question about how I can shorten a Jquery if statement. In my form I have several fields that I check if they are filled. I know there are several plugins to do that for me, but I wan't to learn it by myself (with some help of others ;-))

I got this check

//---- First define hasError
var hasError = false;
//---- Get the value from the inputfield
var firstname = $('#firstname').val();
//---- Do the check
if(firstname == ''){
 $("#error_firstname").show();
 hasError = true;
}else{
 $("#error_firstname").hide();
}

I thought I could write it like this:

(firstname == '') ? $(".firstname").show(): $(".firstname").hide();

And this works, but I can't put the hasError=true in it, so I can't ask this in the end

if(hasError != false) {
 //---- if error, don't refresh page and show errors
 return false;
}else{
 //---- Save values to DB and show succes message
}

Anybody got an idea?

Tnx in advance

Grtzz

Wim

A: 

You can set callback for jQuery show() and hide() methods:

(firstname == '') ? $(".firstname").show(function(){ hasError = true; }): $(".firstname").hide();

rochal
Hi RochalTnx for the quick answer, I'll try your answer, but BalusC started me to do some big thinking, and I think I go for his answer. But tnx again!!
Wim Selles
+8  A: 

To start, refactor it to a function so that you can reuse it for all fields.

Example:

function check(fieldname) {
    var value = $('#' + fieldname).val();
    var error = $('#error_' + fieldname);
    if (value == '') {
        error.show();
        $.hasError = true;
    } else {
        error.hide();
    }
}

so that you can use it as follows:

check('firstname');
check('lastname');
// etc..

Of course you can refactor that again further by storing all names in an array or just by getting $(':input') and calling check() in a loop. You keep busy refactoring ;) You can eventually take a look how existing form validation plugins do it. For example the jQuery Validator.

Whole point is: don't duplicate code. Refactor it.

BalusC
+1 for including link to the jQuery validation plugin. Using this plugin (if possible) would be the sensible choice.
tvanfosson
Hi BalusC, tnx for your answer. You started me do do some big thinking about how I want to set up my validation. I think I take your way!!!!
Wim Selles
@tvanfosson: I know it may be smarter to use the plugin. But If I alsways use plugins, I don't learn hwo to write my own code ;-)
Wim Selles
@Wim Selles -- trust me, you'll find plenty of opportunities to write your own code: event handlers, extending plugins -- say with custom validation rules, etc. No need to duplicate an excellent, already existing plugin, unless you are doing it purely as a learning exercise.
tvanfosson
A: 

Well, I would have recommend you to use JQuery Validation plugin but I'll take it that you want to learn it the 'hard' way. :P

Have the error messages adopt the same class e.g. "error"

<span class="error" id="err_firstname">...</span>
<span class="error" id="err_lastname">...</span>
<span class="error" id="err_email">...</span>

If any of the span.error:visible has at least one item, then hasError = true

 $("span.error:visible").each(function (i) {
    hasError = true;
    return;
  });

The hasError flag check can be shortened too:

if(hasError) {

 //---- if error, don't refresh page and show errors

 return false;
}
else {

 //---- Save values to DB and show succes message

}
o.k.w
I know it may be smarter to use the plugin. But If I alsways use plugins, I don't learn hwo to write my own code ;-)
Wim Selles
Yea, way to go! Looking forward to a Selles' plugin!!!
o.k.w