jQuery Validation not getting options

半城伤御伤魂 提交于 2019-12-25 02:53:54

问题


I'm trying to customise my form validation with no success.

Nor message nor custom error class is firing when I test. Still, it seems to work, since it shows the standard error message after the invalid field.

My code:

HTML

<form role="form" method="POST" id="account-edit">
            <div class="box-body">
                <div class="form-group">
                    <label for="address">Address</label>
                    <input type="text" class="form-control" id="address" name="address" placeholder="Endereço">
                </div>
                <div class="form-group">
                    <label for="telephone">Telephone</label>
                    <input type="text" class="form-control" id="telephone" name="telephone" placeholder="Telefone">
                </div>
                <div class="form-group">
                    <label for="telephone">Contact-email</label>
                    <input type="email" class="form-control" id="email" name="contact_email" placeholder="E-mail">
                </div>
                <div class="form-group">
                    <label for="telephone">Website</label>
                    <input type="text" class="form-control" id="website" name="website" placeholder="Website" >
                </div>
            </div>
            <div class="box-footer">
                <button type="submit" class="btn btn-primary">Update Info</button>
            </div>
        </form>

Javascript:

 $('#account-edit').validate({
            errorClass: "has-error",
            rules: {
                email: {
                    email: true
                }
            },
     messages: {
         email: {
             email: 'Please enter a valid email format: name@domain'
         }
     },
            highlight: function(element, errorClass) {
                $(element).closest('.control-group').addClass(errorClass);
            },
            unhighlight: function(element, errorClass) {
                $(element).closest('.control-group').removeClass(errorClass);
            }
        });

CSS:

.has-error input {
    border: 1px solid red;
}

A JSFiddle with my code: http://jsfiddle.net/ov8zx694/1/

Also, I managed to customize the error message through data-attributes.


回答1:


The rules object can only be constructed using the name attribute. In your case, the name is contact_email, not email...

rules: {
    contact_email: {
        email: true
    }
},
messages: {
    contact_email: {
        email: 'Please enter a valid email format: name@domain'
    }
},

DEMO: http://jsfiddle.net/ov8zx694/2/


Also, the CSS is not working because you don't have any markup that contains the control-group class.

$(element).closest('.control-group').addClass(errorClass);

Either change the markup so that it contains this class or change the class name to something that's already in your markup...

$(element).closest('.form-group').addClass(errorClass);

DEMO 2: http://jsfiddle.net/ov8zx694/3/



来源:https://stackoverflow.com/questions/31253545/jquery-validation-not-getting-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!