Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate a Hidden Field

I'm using MVC3 with unobtrusive validation. I have a field that the user is expected to fill with some data and then press a "search" button. If search has never been pressed or the user has changed the input field after pressing search, the form should not be possible to submit.

I've added a hidden field that is set to true by the click() event of the button and emptied by the keyup() event of the input box. Now I would like to add a validation rule that requires the hidden field to be true to allow submit.

Preferably I would like to use unobtrusive validation, but if that doesn't work it is ok with something that requires some javascript, as long as it doesn't spoil the unobtrusive validation for the rest of the form.

The following code snippet does exactly what I want, until I add type="hidden".

<input class="required" id="client-searched" data-val="true" 
  name="ClientSearched" data-val-required="Press search!"/>
<span class="field-validation-valid" data-valmsg-replace="true" 
  data-valmsg-for="ClientSearched"/>
like image 696
Anders Abel Avatar asked Mar 14 '12 18:03

Anders Abel


2 Answers

try

   var validator = $("#myFormId").data('validator');    
   validator.settings.ignore = "";

Here is an informative blog post

EDIT

@RAM suggested a better solution please FOLLOW

like image 121
Rafay Avatar answered Oct 05 '22 09:10

Rafay


I had a similar problem, and I used this code to change defaults, in MVC 4:

@Scripts.Render("~/bundles/jqueryval")

<script type="text/javascript">
    $.validator.setDefaults({
        ignore: ""
    })
</script>

Source: JQuery validate

like image 27
Francisco Avatar answered Oct 05 '22 09:10

Francisco