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.
You could loop:
But ideally, I would add a class to their parent element and simplify the selector to just this:
EDIT: The code was not complete – I removed a left curly brace.