I gave your script a try and it works well for hiding the address div. This seems a bit cleaner than the script I had for this, thanks!
I still need to figure out validation for this though. Here's my code so far:
- $(document).ready(function(){
- final_size_change(); //show / hide the custom size on start up.
- //new show-hide script
- $('input[name="delivery_method"]').click(function() {
- if($(this).val() == 'Delivery') {
- $("#delivery_address_div").show();
- } else {
- $("#delivery_address_div").hide();
- }
- });
- $("#digital_copy_order_form").validate({
- rules: {
- custom_size: {
- required: function() {
- return $("#final_size :selected").val() == "Custom";
- }
- },
- delivery_address: {
- required: function() {
- return $('input[name="delivery_method"]').val() == "Delivery";
- }
- }
- },
- messages: {
- custom_size: {
- required: "This field is required if you selected Custom Size above."
- },
- delivery_address: {
- required: "This field is required if you selected Delivery above."
- }
- }
- });
- });
The bits about custom_size is for a select menu where if the user selects "custom" for the value, then the final_size text field is made required.
I want to do the same thing with delivery_address. Here I'm attempting to tell the validate script to check delivery_method and see if it's value is "Delivery", if it is... then make delivery_address a required field and use a specific error message if nothing gets entered.
Upon testing, the above script merely makes delivery_address required REGARDLESS of what's checked. So if a user opts to pick in-store pick up for delivery_method, the delivery_address field remains hidden but is still required... essentially blocking form submission.
I know what I have is wrong, probably something with syntax. What am I doing wrong?
-Chris