Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Validation Re: Validation

#92088
jamygolden
Member

You could probably find some sort of validation plugin to help you.

I don’t think what you want to do is difficult, it’s just a bit of work.

$('form').submit( function () {

if ( !$('.checkbox').is(':checked') ) {
return false;
}

});

If you click submit, it will check if .checkbox is checked, if not it will not submit. You could also add errors, etc in that if statement.

“For example, if the user selects a return flight”

$('.some-select').find('option').click( function () {
if ( $(this).attr('value') == 'flight' ) {
$('#some-field').addClass('required').show();
} else {
$('#some-field').removeClass('required').hide();
}
});

If the html was:


If one clicked on the flight option, the #some-field element would appear with a class of “required”. You could use this .required on the submit check.

$('form').submit( function() {
if ( $(this).find('.required').val() ) {
return false;
}
});

If submit is clicked, a quick search for a class of required happens. If any field with this class exists and is empty, the submit will fail.

As you can see, it’s a fair bit of work for a large form without a validation plugin. Did I help answer your question or was I all over the place?