1

I have a simple form that looks like below. I have clicked the Submit button once and all the jquery validation messages appear (as shown in red). However, upon unchecking the Call checkbox for Sports Event, I would like to remove all jquery validate messages associated to the Sports Event only. How can I do that? Currently, if I check the Call checkbox again, the validation messages are still shown.

Please see the codes below:

 //if call checkbox is checked, display the phoneNo and attendeesNo field
    function toggleCall(){
        if (document.getElementById("callCheckbox").checked == true) {
            document.getElementById('phoneNo').style.display = "";
            document.getElementById('attendeesNo').style.display = "";
        } else {
            document.getElementById('phoneNo').style.display = "none";
            document.getElementById('attendeesNo').style.display = "none";
        }
    }

HTML Code:

<tr>
    <td>
        <input type="checkbox" name="callCheckbox" id="callCheckbox" value="true" onclick="toggleCall()" />
    </td>
    <td>
        <input type="checkbox" name="emailCheckbox" id="emailCheckbox" value="true" onclick="toggleEmail()" />
    </td>
    <td> Sports Event 
        <br>
        <div id="sportsEvent" style="display:none;">
            <label> Enter a phone number: </label>
            <input type="text" name="phoneNo" id="phoneNo">
            <br>
            <label> No. of attendees: </label>
            <input type="text" name="attendeesNo" id="attendeesNo">
        </div>
    </td>
</tr>


jquery validation method

<script type="text/javascript">
    //jquery validation Script
    var _global_validator = null;

    $(document).ready(
        function() {
            $("#submitForm").validate( {
                rules: {
                    phoneNo: {
                        required: function (element) {
                                if($("#callCheckbox").is(':checked')){
                                        var e = document.getElementById("phoneNo");
                                        if(e.value==""){
                                            return true;  
                                        }
                                        else{
                                            return false;
                                        } 
                                }
                                else{
                                    return false;
                                }    
                            } 
                      },
                      attendeesNo: {
                        required: function (element) {
                                if($("#callCheckbox").is(':checked')){
                                    var e = document.getElementById("attendeesNo");
                                    if(e.value==""){
                                        return true;  
                                    }
                                    else{
                                        return false;
                                    } 
                                }
                                else {
                                    return false;
                                }  
                        } 
                      },

                    errorElement : "small",

                    invalidHandler : function(e, validator) {
                        _global_validator = validator;
                        //renderDropdownUIValidation(validator);
                        renderCheckableUIValidation(validator);
                    },

                    errorPlacement : function(error, element) {
                        error.insertAfter(element);
                    },

                    messages : {
                        phoneNo: {
                                required: '<Please enter phone number>',
                        },
                        attendeesNo: {
                                required: '<Please enter no. of attendees>',
                        },
                    },

                    submitHandler: function(form) {
                            form.submit();
                    }
                }

            }); //end validate
        });

    </script>

form

1

3 Answers 3

2

You've show absolutely no code whatsoever, so I cannot write a custom solution for your question. I don't even know your validation rules controlling the form. Does clicking the checkbox already change the rules for the text boxes? In other words, does the validation of the text fields depend on the checkbox? Have you written that code at least?

You probably also need to write a change handler function on the checkbox that programmatically forces re-validation of the text field. This assumes that all validation rules are satisfied when the checkbox is un-checked.

$('#yourform').validate({  // <- plugin initialization
    // rules, options, etc.
});

$('#your_checkbox').on('change', function() {
    $('#your_textfield').valid(); // <- force re-validation
});

Otherwise, please post an MCVE so the question can be meaningfully answered.


EDIT:

After OP posted code, it's clear that conditional rules are all that's needed. When the conditional rules are properly declared, the messages will automatically show/hide... there is no need for special code.

There are several other issues...

  1. You've put all of the .validate() method options within the rules object. The rules object is a sibling of the other options. Validation is entirely broken by this.

  2. You cannot put a < or a > within the custom error messages as this is interpreted as an HTML element. Use &lt; and &gt; instead, which are then rendered as < and >.

  3. Something is breaking your invalidHandler option. Fix or remove it.

  4. Remove the inline JavaScript click handlers and replace with jQuery handler functions. You're using jQuery, which renders all inline JavaScript completely obsolete. It's also easier to troubleshoot JavaScript when it's not randomly mixed into your HTML markup.

  5. Remove the getElementById that is within the jQuery code and replace with standard jQuery selectors. Again, you're using jQuery, which normalizes element selection across browsers. document.getElementById('my_element') can simply be replaced with jQuery's id selector, $('#my_element').

  6. Within .validate() rules it makes absolutely no sense to conditionally apply required to a field only when the field itself is empty. If the field is required, then it cannot be left empty in the first place. In your case, required simply needs to be made conditional based on the checkbox.

This demo will point you in the right direction. Click the first checkbox to see the text elements. The error messages will automatically show/hide based on user input thanks to the plugin itself.

http://jsfiddle.net/4v0j181L/

Sign up to request clarification or add additional context in comments.

8 Comments

Hi @Sparky. I have edited the post above by adding in my codes. I just want to clear the validation error messages when people uncheck and then check again the #callCheckbox. Thank you.
@ahgal, no, that's not an accurate description of what you need to do. You just need to properly apply "conditional rules"... then the validation plugin will automatically toggle the error messages.
@ahgal, see my edited answer, which identifies some key issues with your code.
saw your demo. actually what i want is when i uncheck and check again the CallCheckbox, the error messages disappear. something like resetting the response since i restart the action (by unchecking the CallCheckbox) again
@ahgal, I've already given you all the pieces... it's simply up to you to assemble them as per your project. Once the rule is no longer in effect (by unchecking the box), you can force re-validation by using the .valid() method on the text boxes as I've shown in the first part of my answer. I'm sorry I could not simply give you all the code in a finished package, but your example code had so many mistakes that needed fixing first.
|
1
$(document).ready(function () {
})   // inside this you need to write 

1. ## If drop down list ##
$('#serviceType').on('change', function () {
            if ($('#serviceType').text() != '') {
                $('#Error-ServiceType').hide();
            }
        });

2. ## If text box ##
$('#rateperhr').on('keyup keypress change', function () {
            if ($('#rateperhr').val() != '') {
                $('#Err_rateperhour').hide();
            }
        });

1 Comment

Can you add some explanation for your answer? How does this code answer the question?
0
$('.your_form_class_name').validate().resetForm();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.