简体   繁体   English

更改HTML对象属性

[英]Changing HTML Object Property

I have a dynamic form validation event listener, which checks if an input is valid or not. 我有一个动态表单验证事件监听器,它检查输入是否有效。

However, even when the input technically isn't valid, the HTML Object Property validity -> Valid is set to "True". 但是,即使输入技术上无效,HTML对象属性有效性 - >有效也设置为“True”。

I cant figure out how I can trigger it to change between "True" or "False" based on if it's valid or not in my javascript function. 我无法弄清楚如何在我的javascript函数中根据它是否有效来触发它在“True”或“False”之间进行更改。

function validateEventName(){
  const reEventName = /^[a-åA-Å0-9]{2,25}$/;
  if(!reEventName.test(inputEventName.value)) {
    inputEventName.classList.add("is-invalid");
    inputEventName.classList.remove("is-valid");
    inputEventName.checkValidity();
  } else {
    inputEventName.classList.remove("is-invalid")
    inputEventName.classList.add("is-valid")
  }
}

I need to be able to change the inputEventName.valididty.valid between "True" or "False" based on if it's valid or not with the function in my code above. 我需要能够根据上面代码中的函数是否有效来更改“True”或“False”之间的inputEventName.valididty.valid。

use document.getElementById() 使用document.getElementById()

function validateEventName(){
      const reEventName = /^[a-åA-Å0-9]{2,25}$/;
      if(!reEventName.test(document.getElementById("dynamicID").value )) {
        inputEventName.classList.add("is-invalid");
        inputEventName.classList.remove("is-valid");
        inputEventName.checkValidity();
      } else {
        inputEventName.classList.remove("is-invalid")
        inputEventName.classList.add("is-valid")
      }
    }

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

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