简体   繁体   English

Jquery 验证与 HTML 验证相冲突

[英]Jquery validation collide with HTML validation

I want to use jquery validation for simple form using built-in validation method like required and email.我想使用 jquery 验证简单表单,使用内置验证方法,如 required 和 email。

Here is my form这是我的表格

<form id="setup-company" class="form-horizontal form-rest validator" action="{{$updatePath}}">
    <div class="form-group">
        <label for="name" class="col-sm-6 control-label" style="font-size: 12px;text-transform:uppercase">Nama Institusi *</label>
        <div class="col-sm-12">
            <input type="text" class="form-control" name="name" value="{{$company->name}}" style="width: 100%;" required>
        </div>
    </div>
    <div class="form-group">
        <label for="abbreviation" class="col-sm-6 control-label" style="font-size: 12px;text-transform:uppercase">Singkatan</label>
        <div class="col-sm-12">
            <input type="text" class="form-control" name="abbreviation" value="{{$company->abbreviation}}" style="width: 100%;">
        </div>
    </div>
    <div class="form-group">
        <label for="phone" class="col-sm-6 control-label" style="font-size: 12px;text-transform:uppercase">No. Telp</label>
        <div class="col-sm-12">
            <input type="number" class="form-control" minlength="10" maxlength="12" name="phone" value="{{$company->phone}}">
        </div>
    </div>
    <div class="form-group">
        <label for="email" class="col-sm-6 control-label" style="font-size: 12px;text-transform:uppercase">Email</label>
        <div class="col-sm-12">
            <input type="email" class="form-control" name="email" value="{{$company->email}}">
        </div>
    </div>
    <button style="float:right;" type="submit" class="btn btn-sm btn-success btn-raised rippler rippler-inverse edit-row">
        SIMPAN
    </button>
</form>

In the javascript, I initiate jquery validation like this.在 javascript 中,我像这样启动 jquery 验证。

 $('.form-rest').unbind().submit(function(e) {
        e.preventDefault();
        var $form = $(this);
        // check if the input is valid
        if (!$form.valid())
            return false;
});

But when I try it, it shows HTML validation like this但是当我尝试它时,它显示像这样的 HTML 验证

在此处输入图像描述

When I fill invalid input for phone (which is third input), it seems jquery validation working properly.当我为电话填写无效输入(这是第三个输入)时,似乎 jquery 验证工作正常。 And when I try to fill blank input for first input, the error message came from jquery validation like this.当我尝试为第一个输入填充空白输入时,错误消息来自像这样的 jquery 验证。

在此处输入图像描述

Whats the cause of this?这是什么原因?

It's totally fine to have the required attribute.拥有required属性是完全可以的。 You just have to tell the browser that you want to skip the default validation by adding the novalidate attribute to the form element.您只需通过向表单元素添加novalidate属性来告诉浏览器您要跳过默认验证。

The novalidate attribute is a boolean attribute. novalidate 属性是 boolean 属性。 When present, it specifies that the form-data (input) should not be validated when submitted.当存在时,它指定表单数据(输入)在提交时不应该被验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM