Reputation: 73
I am attempting to stop a drupal form from submitting if two fields don't equate. I have two fields, enter your email and confirm email that both need to match prior to allowing the form to submit. the following code is inside of a $(document).read(function() call. How do I do this?
$('#webform-client-form-27 #edit-submitted-confirm-email-address').blur(function() {
if ($('#edit-submitted-email-address').val() == $('#edit-submitted-confirm-email-address').val() ) {
$('#edit-submitted-email-address').css('border', '1px solid' );
$('#edit-submitted-email-address').css('border-color', '#848484 #c1c1c1 #e1e1e1' );
$('#edit-submitted-confirm-email-address').css('border', '1px solid' );
$('#edit-submitted-confirm-email-address').css('border-color', '#848484 #c1c1c1 #e1e1e1' );
}
else {
$('#edit-submitted-email-address').css('border', '2px solid red');
$('#edit-submitted-confirm-email-address').css('border', '2px solid red');
}
});
Upvotes: 0
Views: 99
Reputation: 673
You can do this directly using your form_name_validate function by matching the $form_state values of both textboxes and if value doesnt match you can throw form_set_error function.
Upvotes: 0
Reputation: 597
<form onsubmit="return validateInfoFunction()">
<input type="text" id="email1"/>
<input type="text" id="email2"/>
<input type="submit"/>
</form>
<script type="text/javascript">
function validateInfoFunction(){
if(document.getElementById("email1").value == document.getElementById("email2").value)
{
return true;
}
else return false;
}
</script>
Returning false in an onsubmit event will stop the form submitting. Hope this helps.
Upvotes: 0
Reputation: 445
it's actually quite simple :
$('#myForm').on('submit', function(){
if ($('#theAddress').val() != $('#theConfirmation').val()) {
return false;
}
return true;
});
or if you have just those to check, you can compact it :
$('#myForm').on('submit', function(){
return $('#theAddress').val() == $('#theConfirmation').val();
});
To explain you in details, the return value is used when you add handlers. If you return true
javascript will proceed and submit the form on the submit
event. If you return false
it will simply stop.
Upvotes: 1