admin管理员组

文章数量:1356379

I've searched around for a way to disable the error messages that are displayed next to invalid form elements and I have only found ways to customize the messages, not prevent them altogether.

I tried:

jQuery.extend(jQuery.validator.messages, {
    required: ""
}

But that still places a blank space next to my text boxes and throws off the positioning. Anyone know a way that I can do this?

I've searched around for a way to disable the error messages that are displayed next to invalid form elements and I have only found ways to customize the messages, not prevent them altogether.

I tried:

jQuery.extend(jQuery.validator.messages, {
    required: ""
}

But that still places a blank space next to my text boxes and throws off the positioning. Anyone know a way that I can do this?

Share Improve this question asked Jun 3, 2011 at 6:23 Slappy GriddlesonSlappy Griddleson 953 silver badges10 bronze badges 1
  • manual validation maybe? – Ibu Commented Jun 3, 2011 at 6:41
Add a ment  | 

3 Answers 3

Reset to default 4
$('#submit').click(function(){
    $("#contactform").validate({
        errorPlacement: function(error, element) { },
        debug:true
    })
});

leaving the errorPlacement line blank did the trick.

You could use CSS to hide them

label.error { display:none; }

It's not a perfect solution, but it'll work

I created an over-ride of the existing "required" validator to return no error messages.

Over-Ride Required Method:

// Creates the NEW validator method
jQuery.validator.addMethod('requiredNoMsg', jQuery.validator.methods.required, '');

// Creates a new CSS Rule for the validator method
// so we can add it like "class='requriedNoMsg'".
jQuery.validator.addClassRules('requiredNoMsg', { requiredNoMsg: true });

Full Example:

<html xmlns="http://www.w3/1999/xhtml" >
<head>
    <title>Demo</title>
    <!-- jQuery 1.6.1 -->
    <script type="text/javascript" src="http://code.jquery./jquery-latest.js" charset="utf-8"></script>
    <!-- jQuery Validate 1.8.1 -->
    <script type="text/javascript" src="https://github./jzaefferer/jquery-validation/raw/master/jquery.validate.js" charset="utf-8"></script>
    <script type="text/javascript" src="https://github./jzaefferer/jquery-validation/raw/master/additional-methods.min.js" charset="utf-8"></script>

    <style type="text/css">
        body { margin: 0; padding: 0; font-family: Verdana, Tahoma, Arial, sans-serif; }
        fieldset.main { margin: 1em; padding: 1em; width: 400px; font-size: .9em; }
        fieldset.main ol { padding: 1em 1em 0 1em; list-style: none; }
        fieldset.main li { padding-bottom: .5em; }
        fieldset.main ol li label { display: block; width: 12em; margin-right: 1em; }
        input[type=button] { width: 6em; height: 2.5em; }
        input[type=checkbox] { }
        input[type=text].error { border: 1px dotted red; background-color: yellow;}
    </style>
    <script type="text/javascript">
        jQuery.validator.addMethod('requiredNoMsg', jQuery.validator.methods.required, '');
        jQuery.validator.addClassRules('requiredNoMsg', { requiredNoMsg: true });

        $(document).ready(function () {
            $('#myform').validate(
                {submitHandler: function () { alert('Validation Passed!'); }}
            );
        }); 
        </script>
</head>
<body>
    <form id="myform" method="get" action="">
    <fieldset class="main">
        <ol>
            <li>
                <label for="CustomerCode" class="left">
                    Customer Code</label>
                <input type="text" id="CustomerCode" name="CustomerCode" class="requiredNoMsg" />
            </li>
            <li>
                <label for="DeliveryCode" class="left">
                    Delivery Code</label>
                <input type="text" id="DeliveryCode" name="DeliveryCode" class="requiredNoMsg" />
            </li>
            <li>
                <input type="submit" id="Add" value="Add" />
            </li>
        </ol>
    </fieldset>
    </form>
</body>
</html>

本文标签: javascriptDisabling error messages when using the JQuery validator extensionStack Overflow