Home › Forums › JavaScript › Validation › Re: Validation
Found a way to make it so if the user selects a return flight, they must fill in the return date too. I couldn’t do this using the multi-leg method because I can’t check for “required” fields on the last slide.
So the way I am doing it is I have code it so whether or not the flight is a return, there must be a value in the return field for the form to submit. Then I add to the conditional a bit of code that inserts a word into the return date field if the user has selected a single flight. This means single flights still submit because technically the field has a value, a value we can just ignore.
Then, if the user selects a return flight, the script removes this value so the field is blank so they have to fill it in to submit.
Have to say I think that was a stroke of genius! :P
/*
$(function() {
function checkSingle(select_id){
if($('option:selected', select_id).val() == "Single"){
$('#Return').hide();
$('#retdate').val('3');
} else {
$("#Return").show();
$('#retdate').val('');
}
}
checkSingle('#FlightType');
$("#FlightType").change(function(){
checkSingle(this);
});
});
/* ]]> */
if ( !$('#retdate').val() ) {
$('#errorbox').css('display','block');
$('p.errorbox').replaceWith( "Please complete the return date.
" );
$('#retdate').css('border','1px solid #bc3048');
return false;
}