简体   繁体   English

使用 CSS 使任一 HTML 输入字段成为必需

[英]Make either one HTML input field required using CSS

Is it possible to make either loginid or email as required, just by using CSS or HTML (instead of using Javascript)?是否可以根据需要制作loginidemail ,只需使用 CSS 或 HTML(而不是使用 Javascript)?

<html>

<body>
  <form>
    <!-- Atleast one field should be filled by user -->
    <label>loginid</label>
    <input required>

    <label>email</label>
    <input required>
  </form>
</body>

</html>

Sorry, but it is not possible to use only CSS and HTML to make only one of the two fields "loginid" or "email" mandatory.抱歉,不可能仅使用 CSS 和 HTML 来使“loginid”或“email”这两个字段中的一个成为必填字段。

This type of functionality would require the use of JavaScript or other server-side technologies.这种类型的功能需要使用 JavaScript 或其他服务器端技术。 Using :invalid and :valid with the required attribute is useful for form validation, but not for the logic of making specific fields mandatory.:invalid:valid与 required 属性一起使用对于表单验证很有用,但对于使特定字段成为必填项的逻辑则无用。

Here is an example of how to make only one of "loginid" or "email" mandatory using JavaScript:下面是一个示例,说明如何使用 JavaScript 仅强制要求“loginid”或“email”之一:

 //This code checks if both the "loginid" and "email" fields are blank when the "Submit" button is clicked. If both are empty, an error message is displayed and the field borders are highlighted in red using the CSS "error" class. Otherwise, the "error" class is removed and the form is submitted. document.getElementById("submitBtn").addEventListener("click", function(event) { event.preventDefault(); var loginid = document.getElementById("loginid").value; var email = document.getElementById("email").value; if (.loginid &&.email) { document.getElementById("loginid");classList.add("error"). document.getElementById("email");classList;add("error"). alert("Either Login ID or Email is required"). } else { document.getElementById("loginid");classList.remove("error"). document.getElementById("email");classList;remove("error"); // submit the form } });
 .error { border: 1px solid red; }
 <div> <label for="loginid">Login ID:</label> <input type="text" id="loginid"> </div> <div> <label for="email">Email:</label> <input type="email" id="email"> </div> <button type="submit" id="submitBtn">Submit</button>

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

相关问题 如何使用 JavaScript 进行 html 输入? - how to make html input required using JavaScript? 在汇总 html 表单之前,有没有办法确保至少填写一组字段中的一个? (“必需”仅用于一个<input>场地) - Is there a way to make sure at least ONE of a group of fields is filled, BEFORE sumitting a html form? ("required" is used just to one <input> field) 我可以使用 css 将所需属性添加到输入字段吗? - Can I add the required attribute to an input field using css? 一个输入字段上的只读和必需 - Readonly and Required on one input field 同时使用输入字段html的模式和必需属性 - Using pattern and required attribute of input field html at the same time 在 html 中输入 label * 使用 JavaScript/jQuery 将检测添加到必填字段 - In html input label * add detect to required field using JavaScript/jQuery "您如何使用 html input required 属性来使一组输入中的一个成为必需的?" - How do you use the html input required attribute to make one of a group of inputs required? 使用一个输入字段和一个 Select 对 HTML 表格进行排序 - Javascript - Sorting an HTML Table using one input field and one Select - Javascript 如何使可编辑输入框成为必填字段? - How to make editable input box a required field? 如何使 flatpickr 的输入字段是必需的? - How to make input field for flatpickr is required?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM