Rahul
Rahul

Reputation: 2307

Show validation message for Hidden field

May be this can be a question some more similar to others But I haven't find the solution that I want from those.

I want to show the Validation message I have set for particular field in MVC::

My ViewModel is::

 public class MainDocumentViewModel
    {


        [Required(ErrorMessage = "Please Enter Valid Customer Name")]
        public long CustomerID { get; set; }
    }

My View is as ::

<form>

    @Html.HiddenFor(x => x.CustomerID)
    @Html.ValidationMessageFor(x=>x.CustomerID)

 <button type="submit">Submit</button>
</form>

But after submitting the form, I am not getting any error. Help me on this. Thanks in advance.

Upvotes: 3

Views: 8822

Answers (3)

Ashwini Verma
Ashwini Verma

Reputation: 7525

for validating only for particular hidden field, do this.

<script>
    $.validator.setDefaults({
        ignore: $('#CustomerID') 
    });
</script>

OR for specific form

$('#myform').validate({
        ignore: []
    });

Upvotes: 4

Murali Murugesan
Murali Murugesan

Reputation: 22619

If you are using jQuery validation plugin by default it will ignore hidden fields. To validate hidden fields change the default settings

$.validator.setDefaults({ 
    ignore: [],
    // other default options
});

Upvotes: 2

Amit
Amit

Reputation: 15387

Please go through the Here, it is very helpful

Client side validation for hidden fields is not working, because jQuery validation ignore all hidden tags.

You must define HiddenRequiredValidator class to achieve your goal.

Upvotes: 4

Related Questions