Jquery validation error messages are not hiding, why

user1740381

I am using jquery validation and jquery unobtrusive validation plugins. I am trying to hide error message, but error message are not hiding. I have created a js fiddle:

Fiddle Link

Following is the code which i used in the fiddle:

Html

<form id="form">
    Name:
    <input data-val="true" data-val-length="The field First Name must be a string with a minimum length of 3 and a maximum length of 15." data-val-length-max="15" data-val-length-min="3" name="FirstName" type="text" />
    <span class="field-validation-valid help-block" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
</form>
<button id="hide">Hide</button>

Hide

JS

$("#hide").click(function(){
   $("#form").data("validator").hideErrors();
});

What i am missing here and why hideErrors() function are not working ??

Edit

I don't know why you guys are not keeping jquery unobtrusive validation in mind while giving answers. Almost all answer are just focusing on how to hide error messages without worrying about the existing functionality which could be break if we just hide the message. I already mention that i want the answer of

Why hideErrors() function of jquery validator object is not working ?

I have also created a simplest possible fiddle which demonstrate my problem, check this.

Gaurav

If you want to hide validation error messages, than you should use resetForm function instead of hideErrors function. As other mentioned in their answers, resetForm internally call hideErrors function.

But here's the twist comes, when you will try to use resetForm function it will not work because of the way jquery.validate.unobtrusive plugin works.

jquery.validate.unobtrusive plugin override the errorClass and errorElement property of jquery.validate plugin.

From jquery.validate.unobtrusive plugin code Line-109

    .... 
    if (!result) {
        result = {
            options: {  
                errorClass: "input-validation-error",
                errorElement: "span",
                errorPlacement: $.proxy(onError, form),
    ...

So when you will call resetForm function on validator's object, jquery.validate plugin can't find error labels to remove so validation error message will not remove.

Now you have two options to remove error messages:

Option - 1

You can edit jquery.validate.unobtrusive plugin code and replace errorClass and errorElement values with the following values:

    errorClass: "error",
    errorElement: "label",

These are the default values which jquery.validate plugin use.

Option - 2

You can write your own code which will do the trick and by this way you do not have to modify the plugin code. Here is the code which you can use with little modifications mentioned below:

        // get the form 
        var form = $("#formId");

        // get validator object
        var validator = form.validate();

        // get errors that were created using jQuery.validate.unobtrusive
        var errors = form.find(".field-validation-error span");

        // trick unobtrusive to think the elements were succesfully validated
        // this removes the validation messages
        errors.each(function () { validator.settings.success($(this)); })

        //this is optional, only needed if you defined 
        //custom unhighlight function  
        if (validator.settings.unhighlight) {
            for (i = 0, elements = validator.invalidElements() ; elements[i]; i++) {
                validator.settings.unhighlight.call(validator, elements[i], validator.settings.errorClass, validator.settings.validClass);
            }
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery validation - show field name in error messages

From Dev

Adding custom error messages using Jquery Validation

From Dev

Jquery custom validation messages

From Dev

Jquery custom validation messages

From Dev

Why does simple form not show validation error messages in Rails?

From Dev

How to change `Email validation` error messages dynamically in jquery

From Dev

How to change `Email validation` error messages dynamically in jquery

From Dev

Customize min and max error messages for jQuery unobtrusive validation

From Dev

Jquery - form validation, add css styles to the error messages

From Dev

jquery validation error messages using bootstrap does't work properly

From Dev

jquery-validation-unobtrusive: Display Different Error Messages in Summary and Label

From Dev

Bootstrap Validation Error Messages CSS

From Dev

Implementing data validation with error messages

From Dev

php form validation and error messages

From Dev

Why stdout for error messages

From Dev

Why stdout for error messages

From Dev

jQuery Validation Fields:getting error messages to disappear upon entering valid information

From Dev

Error in hiding/showing div using jQuery

From Dev

Jquery Validation messages as a placeholder not clearing as you type

From Dev

Jquery validation working with class but not with rules and messages

From Dev

Jquery Validation messages as a placeholder not clearing as you type

From Dev

jQuery Validation Plugin not showing custom messages

From Dev

String Validation Error in jQuery

From Dev

Form validation Jquery no error

From Dev

jQuery Validation syntax error

From Dev

Error placement in jquery validation

From Dev

How to use different error codes for validation messages?

From Java

show validation error messages on submit in angularjs

From Dev

Ruby on Rails get validation error messages on controller

Related Related

  1. 1

    JQuery validation - show field name in error messages

  2. 2

    Adding custom error messages using Jquery Validation

  3. 3

    Jquery custom validation messages

  4. 4

    Jquery custom validation messages

  5. 5

    Why does simple form not show validation error messages in Rails?

  6. 6

    How to change `Email validation` error messages dynamically in jquery

  7. 7

    How to change `Email validation` error messages dynamically in jquery

  8. 8

    Customize min and max error messages for jQuery unobtrusive validation

  9. 9

    Jquery - form validation, add css styles to the error messages

  10. 10

    jquery validation error messages using bootstrap does't work properly

  11. 11

    jquery-validation-unobtrusive: Display Different Error Messages in Summary and Label

  12. 12

    Bootstrap Validation Error Messages CSS

  13. 13

    Implementing data validation with error messages

  14. 14

    php form validation and error messages

  15. 15

    Why stdout for error messages

  16. 16

    Why stdout for error messages

  17. 17

    jQuery Validation Fields:getting error messages to disappear upon entering valid information

  18. 18

    Error in hiding/showing div using jQuery

  19. 19

    Jquery Validation messages as a placeholder not clearing as you type

  20. 20

    Jquery validation working with class but not with rules and messages

  21. 21

    Jquery Validation messages as a placeholder not clearing as you type

  22. 22

    jQuery Validation Plugin not showing custom messages

  23. 23

    String Validation Error in jQuery

  24. 24

    Form validation Jquery no error

  25. 25

    jQuery Validation syntax error

  26. 26

    Error placement in jquery validation

  27. 27

    How to use different error codes for validation messages?

  28. 28

    show validation error messages on submit in angularjs

  29. 29

    Ruby on Rails get validation error messages on controller

HotTag

Archive