1

I have a hidden field that gets populated (via Jquery) when a user clicks on a thumbnail image (with a class of 'thumbnail') using

<input type="hidden" id="DocThumbnail" name="DocThumbnail" value=""/>

and

$(".Thumbnail").click(function() {
    var thumbnailFile = $(this).attr("src");
    $("#DocThumbnail").val(thumbnailFile);
});

I have set up my jquery validation as follows:

$("#AddDocumentForm").validate({
    ignore: [], 
    rules: {
        DocThumbnail : { required : true}
    },
    messages: {
        DocThumbnail: "Please choose a thumbnail"
    }
});

When I click the submit button on the AddDocumentForm the error appears as the value of the hidden field is empty, however when I click on a thumbnail (with a class of 'thumbnail') then hidden field is populated but the validation alert stays on so the form will not submit.

Does anybody know how I can validate a jquery populated hidden field as if I don't add the ignore:[]; line then the hidden field is ignored in the validation

2
  • if you're using version 1.9 or higher of the plugin, have you seen this: stackoverflow.com/questions/8466643/…? Commented Oct 28, 2015 at 11:20
  • using version 14 and yes I have seen that post but even adding $.validator.setDefaults({ ignore: '' }); outside of document.ready did not get it working Commented Oct 28, 2015 at 11:32

1 Answer 1

1

You need to call the Validator.element method after you change the value of DocThumbnail. That tells jQuery Validate to re-check DocThumbnail which will make the error message disappear.

Your updated code would look like this:

var validator = $("#AddDocumentForm").validate({
    ignore: [], 
    rules: {
        DocThumbnail : { required : true}
    },
    messages: {
        DocThumbnail: "Please choose a thumbnail"
    }
});

$(".Thumbnail").click(function() {
    var thumbnailFile = $(this).attr("src");
    $("#DocThumbnail").val(thumbnailFile);
    validator.element($('#DocThumbnail'));
});

You can see a working example here: http://jsfiddle.net/ryleyb/r0rkfykg/

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

1 Comment

Thanks Ryley, I hacked it by using Jquery within the $(".Thumbnail").click(function() to remove the error label created by the validation (if one existed) but your method is cleaner

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.