Ravi Ram
Ravi Ram

Reputation: 24488

JQuery validation - Highlight and message refactor

I have the folliwng code working, however I feel that there is a cleaner way of writing it.

<script type="text/javascript">     
    $(document).ready(function () {        
        $("#formRequest").validate({
            highlight: function (element, errorClass) {
                $(element).addClass("rfvTB");
            },
            unhighlight: function (element, errorClass) {
                $(element).removeClass("rfvTB");
            } 
        });

        $.validator.messages.required = '  *';
    }); 
</script>

the validation message seems like it belongs within the validate function. Is there a way of writing this cleaner?

Upvotes: 1

Views: 237

Answers (1)

Ryley
Ryley

Reputation: 21226

There isn't a wonderful way to do that without a bunch of statements inside your validate object:

You can specify the messages option in validate, and then for each required element, add a required: '*', like this:

$('#formRequest').validate({
   //your options
   messages: {
      formElementName1: {
         required: '*'
      },
      //repeat for each form element
   }
});

If you have a lot of elements, this is pretty tedious and I would prefer your method.

Upvotes: 1

Related Questions