-1

I am attempting to use the value of a checkbox on a form to control the validation of other fields. But it seems that the value is always false inside the addmethod routine. I must be doing something wrong here but I can't see it.

My addmethod routine is this:

$.validator.addMethod('ValShort', function (value,element) 
{ 
  var isChecked = $("value").is(":checked");
  alert(isChecked);
  return true;
}, 'Please enter a rate for short sessions.');

I set the rule up like this:

rules: {
  Test: {ValShort: true}
},

Here is the form:

<form id="Modal-Update-Form" method="post">
    <table align="left" width="100%">
        <tr><td>test</td><td><input type="checkbox" name="Test" id="Test"  /><td></tr>
        <tr><td colspan="2" style="padding-bottom:3px;">
                <button type="submit" class="Button" name="Update">Update</button>
            </td></tr>
    </table>
</form>

No matter what I do, the checkbox always shows as false in the alert.

Any ideas on what I am doing wrong, or in how to get the checkbox state inside the addmethod routine?

6
  • 2
    $("value") selects an element like <value>. There is no such element in your form. Commented Apr 6 at 16:44
  • Your validation rule is on the checkbox Test. You said you wanted to use the rule to control validation of other fields. Commented Apr 6 at 16:46
  • "value" is the parameter that comes into the function from jQuery validation and would be Test in this usage. What notation should I use? Commented Apr 6 at 17:11
  • if (element.checked) Commented Apr 6 at 17:22
  • But this is still not using the checkbox when validating another field. It's essentially the same as Test: required Commented Apr 6 at 17:27

1 Answer 1

0

The alert (isChecked) always returns false, even if the checkbox is flagged. This means the code can't determine the true state of the checkbox.

Wrong :

$("value").is(":checked");

Correct :

$("#Test").is(":checked");

Make Sure You Have $("#Test").

and if you don't want it for a specific element :

var isChecked = $(element).is(":checked");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.