简体   繁体   English

无法设置cuctom html5验证消息

[英]Unable to set cuctom html5 validation message

I am trying to use browser validation messaging. 我正在尝试使用浏览器验证消息。 I am not able to define a custom message when 2 fields are NOT identical. 当2个字段不相同时,我无法定义自定义消息。

My situation is that i have to test when the field IS valid, so i used jquery on.blur to test, but for some reason, the message is empty. 我的情况是我必须测试字段是否有效,所以我使用jquery on.blur进行测试,但由于某种原因,消息是空的。

Try entering 2 valid but different email addresses. 尝试输入2个有效但不同的电子邮件地址 The expected result is supposed to be "Test why not working" has a browser message. 预期的结果应该是“测试为什么不工作”有浏览器消息。

Working jsFiddle of my situation 工作jsFiddle我的情况

HTML: HTML:

<form id="newsletter_form" class="form" role="form" name="newsletter_form" method="post" action="/" autocomplete="off">
    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
            <input type="email" class="form-control" name="email" id="email" value="" maxlength="250" placeholder="email" aria-required="true" required="required" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
            <input type="email" class="form-control" name="confirm_email" id="confirm_email" value="" maxlength="250" placeholder="confirm email" aria-required="true" required="required" />
        </div>
    </div>
    <input type="submit" class="btn submit btn-default" value="submmit!" />
</form>

JAVASCRIPT: JAVASCRIPT:

$('form #email').on('change invalid', function () {
    var field = $(this).get(0);
    field.setCustomValidity('');
    if (!field.validity.valid) {
        field.setCustomValidity("custom email invalid");
    }
});


/* THIS IS MY BUGGY ONE */
$('form #confirm_email').on('blur valid', function () {
    var field = $(this).get(0);
    field.setCustomValidity('');
    if ($("#email").val() != $("#confirm_email").val()) {
        /* i tried setting the field to invalid, nothing */
        //field.validity.invalid;
        //alert('s');
        field.setCustomValidity("test why not working");
    }
});

$('form #confirm_email').on('change invalid', function () {
    var field = $(this).get(0);
    field.setCustomValidity('');
    if (!field.validity.valid) {
        field.setCustomValidity("custom error message");
    }
});

I tried jquery.on(change valid .. and so on, i get the alert() bot not the changed text. 我尝试了jquery.on(更改有效..等等,我得到alert() bot而不是更改的文本。

Any help will be appreciated, i am getting bored of this 'bug'? 任何帮助将不胜感激,我对这个'错误'感到厌倦?

but for some reason, the message is empty. 但由于某种原因,消息是空的。

That is because you are explicitly setting it to be empty: 那是因为你明确地将它设置为空:

 field.setCustomValidity(''); 

in your "custom error message" handler that is used to grump about the invalid email. 在您的“自定义错误消息”处理程序中,用于破坏无效的电子邮件。 That handler is executed after the 'blur valid' , and will always reset what happened before. 该处理程序在'blur valid'之后执行,并且将始终重置之前发生的事情。 You can try it by uncommenting above line. 您可以通过取消注释以上行来尝试它。

Any help will be appreciated 任何帮助将不胜感激

Check both reasons in the same handler: 在同一个处理程序中检查两个原因:

$('form #confirm_email').on('change valid invalid', function () {
    // var field = $(this).get(0); -- don't do this!!! field = this.

    this.setCustomValidity('');
    if (!this.validity.valid) {
        this.setCustomValidity("custom message for standard errors");
    } else if ($("#email").val() != this.value) {
        this.setCustomValidity("test now working");
    }
});

( updated demo ) 更新演示

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

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