简体   繁体   English

如何为不同的输入设置不同的自定义有效性

[英]How to set a different custom validity for different inputs

Can the javascript below to individually have different custom validity depending on what information is actually being entered in the field. 下面的JavaScript可以根据实际在字段中输入的信息来单独具有不同的自定义有效性。 eg. 例如。 For Name: "please enter a name". 对于名称:“请输入名称”。 For Location: "please enter a location. The loop part in the script is confusing me a little! 对于位置:“请输入一个位置。脚本中的循环部分使我有些困惑!

HTML HTML

<form id="sidebarform" onsubmit="return false" method="post" name="myForm" >
   <input type="text" name="name" id="username" placeholder="Name (eg. Rob James)"required><br>
   <input type="text" name="location" id="userlocation" placeholder="Location (eg. Wacol)" required><br>
   <input type="submit" id="sidebarformsubmit" value="Submit">
</form> 

JAVASCRIPT JAVASCRIPT

<script >
$(document).ready(function() {
    var elements = document.getElementsByTagName("INPUT");
     for (var i = 0; i < elements.length; i++) {
       elements[i].oninvalid = function(e) {
         e.target.setCustomValidity("");
        if (!e.target.validity.valid) {
            e.target.setCustomValidity("Please enter a name.");

        }
    };
    elements[i].oninput = function(e) {
        e.target.setCustomValidity("");
    };
  }
 })
 </script>

I was following this question for the initial custom validity. 我遵循此问题的最初的自定义有效性。

HTML5 form required attribute. HTML5表单必填属性。 Set custom validation message? 设置自定义验证消息?

How about using a selector other than just getting everything by tagName, and then set whatever validity you want 除了通过tagName获取所有内容,然后设置所需的有效性以外,如何使用选择器

$(document).ready(function () {
    $('#username').on({
        invalid: function (e) {
            e.target.setCustomValidity("");
            if (!e.target.validity.valid) {
                e.target.setCustomValidity("Please enter a name.");
             }
        },
        input: function(e) {
            e.target.setCustomValidity("");
        }
    });

    $('#userlocation').on({
        invalid: function (e) {
            e.target.setCustomValidity("");
            if (!e.target.validity.valid) {
                e.target.setCustomValidity("Please enter a location.");
             }
        },
        input: function(e) {
            e.target.setCustomValidity("");
        }
    });
});

FIDDLE 小提琴

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

相关问题 如何根据两个不同的输入动态设置链接的路径? - How to dynamically set the path of a link based on two different inputs? 如何将来自2个不同输入的值推入像这样的数组集[“ one”,2] - How to push the values from 2 different inputs into an array set like this [“one”, 2] 表单验证集自定义有效性 - Form Validation Set Custom Validity 如何用不同的输入测试功能 - How to test a function with different inputs 如何在指令(AngularJS)中设置有效性 - How to set validity in directive (angularjs) 如何为同一页面上的不同输入和文本区域设置动态字符计数器,每个最大长度不同? - How do I set up a dynamic character counter for different inputs and textareas on the same page with different max lengths on each one? 在AngularJS中单击按钮后使用指令在其他输入上设置有效性 - Set Validity on other inputs using directives upon button click in AngularJS 如何从具有多个对象的平面列表中设置和获取不同的状态和输入 - How do i set and get the different states and inputs from a flatlist with multiple objects 如何分别管理2个不同输入的状态 - How to manage state separately of 2 different inputs 如何将不同类型的表单输入附加到 formData()? - How to append different types of form inputs to formData()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM