检查同一功能中多个输入字段的长度不同

时间:2019-03-28 07:58:22

标签: javascript

解决了!! 我已经更改了一点代码,我认为现在更正确了,现在我已将事件对象用于目标模糊的输入。

    var inputHandler = document.getElementsByTagName("input")[0];
    var inputListener = document.getElementsByTagName("input")[1];
    var msg = document.getElementById("message");

    function checkLength(e, minLength){
        el = e.target;
        if (el.value.length < minLength) {
            msg.textContent = "please, introduce at least " + minLength + " characters";
            el.style.borderColor = "red";
        }
        else{
            msg.textContent = "";
            el.style.borderColor = "green";
        }
    }

    inputHandler.onblur = function(e){checkLength(e, 3);};
    inputListener.addEventListener("blur", function(e){checkLength(e, 5);}, false);

1 个答案:

答案 0 :(得分:0)

尝试:

function checkLength(minLegth){
    if (inputHandler.blur) {
      if (inputHandler.value.length < minLegth){
        message.textContent = "tiene que tener al menos " + minLegth + " caracteres";
        inputHandler.style.borderColor = "red";
      }
      else{
      message.textContent = "";
      inputHandler.style.borderColor = "green";
      }
    }
    if (inputListener.blur) {
      if (inputListener.value.length < minLegth){
        message.textContent = "tiene que tener al menos " + minLegth + " caracteres";
        inputListener.style.borderColor = "red";
      }
      else{
        message.textContent = "";
        inputListener.style.borderColor = "green";
      }
    }
    else{}
    }