Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript check multiple inputs for blank, return one value

Ok guys, before I continue, I know what I want to do could be done with numerous variables etc. but I've gotta imagine that what I'm doing can be done much more simply. So what I have is a form that has 4 fields and I want to check if anyone of the four are blank. Then each one that is blank, I want to add a class and use the jquery UI effect 'shake' to notify. Then I want to get a true or false response, true being that all aren't blank and false being that any one of the 4 is blank. so what I have is.. HTML...

<form name = "regin" id = "regin">
  <div id = "form_hold">
  <div><label>Username</label><input type="text" id = "Rusername" name = "Rusername" /><div class = 'verdiv' id = 'rvuname'></div></div>
  <div><label>Email</label><input type="text" id = "Remail" name = "Remail" /><div class = 'verdiv' id = 'rvemail'></div></div>
  <div><label>Password</label><input type="password" id = "Rpassword" name = "Rpassword" /><div class = 'verdiv' id = 'rvpass1'></div></div>
  <div><label>Confirm</label><input type="password" id = "Rpassword2" name = "Rpassword2" /><div class = 'verdiv' id = 'rvpass2'></div></div>
  </div>
  <button type="button" class = "thoughtbot" id = "registerButton">Register</button>
  </form>

and the javascript/jquery...

if($username == ''){
    $('#Rusername').parent().find('.verdiv').addClass('error');
    $('#Rusername').parent().effect("shake", { times:3 }, 50);
}
if($email == ''){
    $('#Remail').parent().find('.verdiv').addClass('error');
    $('#Remail').parent().effect("shake", { times:3 }, 50);
}   
if($password == ''){
    $('#Rpassword').parent().find('.verdiv').addClass('error');
    $('#Rpassword').parent().effect("shake", { times:3 }, 50);
}
if($checkval == ''){
    $('#Rpassword2').parent().find('.verdiv').addClass('error');
    $('#Rpassword2').parent().effect("shake", { times:3 }, 50); 
}

Now is there one top level function to 'test' each one of these statements to see if they're true? Thanks.

like image 257
Greg Thompson Avatar asked Nov 09 '11 16:11

Greg Thompson


2 Answers

You could loop:

$('#Rusername, #Remail, #Rpassword, #Rpassword2').each(function() {
  if ($(this).val() == '') {
    $(this).parent().effect('shake', {times: 3}, 50).find('.verdiv').addClass('error');
  }
});

But ideally, I would add a class to their parent element and simplify the selector to just this:

$('.parent_class input');

EDIT: The code was not complete - I removed a left curly brace.

like image 121
Blender Avatar answered Sep 21 '22 19:09

Blender


Put the same class on each of the input fields to be validated, for example validatefield.

Then assuming that $username and $email are the values of each input field, this should work:

var valid = true;
$(".validatefield").each(function() {
    if ($(this).val() == "") {
        $(this).parent().find('.verdiv').addClass('error');
        $(this).parent().effect("shake", { times:3 }, 50);
        valid = false;
    }
});
like image 24
Rory McCrossan Avatar answered Sep 22 '22 19:09

Rory McCrossan