I'm using the following code to validate input fields, and some additional code to validate radio buttons and checkboxes. To deal with the radios/checkboxes I'm using browser alerts (they proved to be too difficult to style with highlighting). What I'd like to do is check all the input fields first, and then validate the radio/check fields so that the alerts don't appear until after everything else has been filled out. Otherwise, the alert box appears in the browser on top of all the highlighted input fields. Too messy. Does anyone have an idea of how I can combine the two scripts and modify the code to order the validation of elements?
- $(document).ready(function() {
// Adding an additional method...
$.validator.addMethod("alpha", function(value,element)
{
return this.optional(element) || /^[a-zA-Z]$/i.test(value);
}, "Letters only");
// Validate signup form on key-up and submit
$("#signupForm").validate({
errorClass: "error",
highlight: function(element, errorClass, validClass) {
$(element).css({ backgroundColor: '#ff7800' });
$(element).closest("input")
.addClass(errorClass)
.removeClass(validClass);
},
unhighlight: function(element, errorClass, validClass) {
$(element).css({ backgroundColor: '#ffffff' });
$(element).closest("input")
.removeClass(errorClass)
.addClass(validClass);
},
errorPlacement: function() {
// Adding this function overrides the original Validator function
// And since nothing is specified, no errors messages are displayed
},
rules: {
first_name: {
required: true,
minlength: 2
},
middle_init: {
// accept: "[a-zA-Z]+"
alpha: true
},
last_name: {
required: true,
minlength: 2
},
address_1: {
required: true,
minlength: 5
},
city: "required",
state: "required",
country: "required",
postal: "required",
confirm_email: {
required: true,
email: true,
equalTo: "#email"
},
email: {
required: true,
email: true
},
phoneAreaCode: "required",
phoneMiddle: "required",
phoneLast: "required",
//fellowship: "required",
participation: "required"
},
});
});
And within the HTML doc:
- <script type='text/javascript'>//<![CDATA[
$(document).ready(function()
{
$("#signupForm").submit(function()
{
// In order for ID validation to work, checkbox IDs within a group are set to the same value, not to unique values:
//if (!isCheckedById("setting"))
// OR, do the following:
if (!isCheckedById("setting_checkbox_0") && !isCheckedById("setting_checkbox_1") && !isCheckedById("setting_checkbox_2") && !isCheckedById("setting_checkbox_3"))
{
alert ("Please select at least one preferred setting.");
return false;
}
//else if (!isCheckedById("practice"))
else if (!isCheckedById("practice_checkbox_0") && !isCheckedById("practice_checkbox_1") && !isCheckedById("spractice_checkbox_2") && !isCheckedById("practice_checkbox_3"))
{
alert ("Please select at least one practice type.");
return false;
}
else if (!isCheckedById("fellowship_yes") && !isCheckedById("fellowship_no") )
{
alert ("Please tell us whether or not you are pursuing a fellowship.");
return false;
}
else
{
return true; //submit the form
}
});
function isCheckedById(id)
{
var checked = $("input[id="+id+"]:checked").length;
if (checked == 0)
{
return false;
}
else
{
return true;
}
}
function isCheckedByName(inputName)
{
var checked = $("input[name="+inputName+"]:checked").length;
if (checked == 0)
{
alert ("Please select at least one.");
return false;
}
else
{
return true;
}
}
});
//]]>
</script>