简体   繁体   English

添加到浏览器的内置表单验证中

[英]Adding onto browsers' built-in form validation

I'm creating an email form for our website. 我正在为我们的网站创建一个电子邮件表单。 I built the form using HTML5 and CSS to take advantage of browsers' built-in validation (using this shim to avoid compatibility problems). 使用HTML5和CSS构建了表单,以利用浏览器的内置验证功能 (使用此填充程序可避免兼容性问题)。 Now I want to add a "confirm your email address" field, which shouldn't be marked valid unless it matches the "enter your email address" field. 现在,我要添加“确认您的电子邮件地址”字段,除非该字段与“输入您的电子邮件地址”字段匹配,否则不应将其标记为有效。

I've written some JavaScript to compare the two fields. 我已经编写了一些JavaScript来比较这两个字段。 Is there a way for JavaScript to mark a field as valid/invalid for the browser's built-in validation? JavaScript有没有办法将字段标记为对浏览器的内置验证有效/无效? Or do I need to switch to a third-party validation plugin to get this feature? 还是我需要切换到第三方验证插件才能获得此功能?

Found the answer right after I posted the question. 我发布问题后立即找到答案 You call <element>.setCustomValidity() on the field, passing in an empty string to set the field as valid. 您在字段上调用<element>.setCustomValidity() ,传入一个空字符串以将该字段设置为有效。 If it's not valid, you instead pass in a string with the reason why. 如果无效,则改为输入原因。

Here's my code: 这是我的代码:

$('#emailConfirmation').on('keyup', function() {
    if ($('#emailConfirmation').val() == $('#email').val()) {
        $("#emailConfirmation")[0].setCustomValidity("");
    } else {
        $("#emailConfirmation")[0].setCustomValidity("Check that you've typed your email address the same way in both places.");
    }
});

Here it is again without jQuery: 这又是没有jQuery的情况:

document.getElementById('emailConfirmation').onchange = function() {
    if (document.getElementById("emailConfirmation").value == document.getElementById("email").value) {
        document.getElementById("emailConfirmation").setCustomValidity("");
    } else {
        document.getElementById("emailConfirmation").setCustomValidity("Check that you've typed your email address the same way in both places.");
    }
}

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

相关问题 使用内置样式的Bootstrap 3表单验证 - Bootstrap 3 Form Validation Using built-in Styles 使用内置的Magento表单验证将单选框设置为必填项 - Set a radio box as a required entry with the built-in Magento form validation 内置 HTML5 个人字段集的表单验证? - Built-in HTML5 form validation for individual fieldset? 在复选框更改时提交表单并保持内置浏览器验证 - Submit form on checkbox change and keep built-in browser validation 浏览器中常见的内置javascript异步函数有哪些? - What are some common built-in javascript asynchronous functions in browsers? 为什么旧浏览器不支持 JavaScript 中的新内置方法? - Why are new built-in methods in javascript not supported by older browsers? 隐藏浏览器内置的查找功能中的DIV元素 - Hiding DIV elements from browsers built-in find function 使用内置验证来验证在Magento中单击/选择的按钮 - Validate for a button click/selected in Magento using the built-in validation vuelidate - 如何使用内置验证器显示自定义验证消息 - vuelidate - How to display custom validation message with built-in validators 向Array添加自定义方法时的最佳实践(内置对象) - Best practice when adding custom method to Array (Built-in object)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM