Take the 2-minute tour ×

I have a form that saves in two modes, a submit mode where all required fields are validated as such, and a 'Draft' mode where I ignore all required fields and submit the form, validating all other rules. I am using jquery 1.8.0 This code on the submit button used to work fine:

var settngs = $.data($('form'), 'validator').settings; 
settngs.ignore = '[data-val-required]';

I then added the following code to toggle the required validation on some fields, depending on selections by the user. It seems to work correctly and removes required correctly, however the submit button code no longer works and insists on validating the remaining required fields...

function toggleRequired(targetname, state) {

var target = $('#' + targetname);

if (state) {
    //if it's already there, we don't want to add it
    if (target.attr('data-val-required') == undefined) {
        //do we have the original validation message?
        if (target.attr('reqMsg') !== undefined)
            target.attr('data-val-required', target.attr('reqMsg'));
            target.attr('data-val-required', targetname + " is required.");
        target.attr('data-val', 'true');
        //set the red required astrick flag on the field
        $("span[for=" + targetname + "]").text("*");
    //if we haven't set the backup copy of the validation message previously, then set it now.
    if (target.attr('reqMsg') == undefined) {
        target.attr('reqMsg', target.attr('data-val-required'));
    $("span[for=" + targetname + "]").empty();

//drop the pre-parsed validation rules and reparse the form

share|improve this question
Why don't you just validate with jquery validate programmatically, and just validate the form when you need to? –  Jack Dec 4 '12 at 19:29
I'd like any other validation rules to still apply, aside from required. Just not sure why it stopped working, perhaps I am no longer referencing it correctly since I reparsed? I just don't know as I am somewhat new to jquery... –  Dan Benson Dec 4 '12 at 19:44
Getting any console errors? F12 usually –  Jack Dec 4 '12 at 19:45
The original button code started tossing a null error on the settings object. Tried this instead, no error, but not the hoped for behavior - $.validator.defaults.ignore = '[data-val-required]'; –  Dan Benson Dec 4 '12 at 19:51
Setting up a jsfiddle can help us tinker with it. –  Jack Dec 4 '12 at 20:03
add comment


Your Answer



Browse other questions tagged or ask your own question.