简体   繁体   English

为什么此javascript验证总是失败

[英]Why is this javascript validation always failing

I've got a classic input validation for an email and a password, but for some reason my logic doesn't work and I can't figure out the part where it fails. 我已经对电子邮件和密码进行了经典的输入验证,但是由于某种原因,我的逻辑不起作用,并且我无法弄清失败的部分。 This is code I've refactored from some one else and it bugging me quite a lot now 这是我从其他代码重构而来的代码,现在困扰着我很多

function Validation() {
  this.regex = {
    email: new RegExp(/[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9-]+/),
    password: new RegExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,13}/),
  },
  this.email = function () {
    let valElement = document.getElementById("mail");
    console.log(valElement);
    let valueR = valElement.value.toString();
    console.log(valueR);
    let erorrEl = document.getElementById("error");

    let validate_mail = valueR.length == 0 && this.regex.email.test(valueR);
    console.log(validate_mail);
    console.log(this.regex.email);
    if(!validate_mail){
      valElement.className = "invalid";
      erorrEl.innerHTML = "Please enter a correct email";
      erorrEl.className = "error active"
      console.log("not validated email");
      event.preventDefault();
    }else{

      valElement.className = "valid";
      erorrEl.innerHTML = "";
      erorrEl.className = "error"
      console.log("validated email");
    }
  }
  this.password = function () {
    let valElement = document.getElementById("pass");
    let valueR = valElement.value;
    let erorrEl = document.getElementById("error2");
    let validate_pass = valueR.length == 0 && this.regex.password.test(valueR);
    if(!validate_pass){
      valElement.className = "invalid";
      erorrEl.innerHTML = "Please enter a correct password";
      erorrEl.className = "error active"
      console.log("not validated password");
      event.preventDefault();
    }else{
      valElement.className = "valid";
      erorrEl.innerHTML = "";
      erorrEl.className = "error"
      console.log("validated password");
    }
  }
  this.form = function(){
    if(this.password && this.email){

    }else{

    }
  }
}

var Valdator = new Validation();
        Valdator.email();
        Valdator.password();

Usually I'm calling these Valdator email and password functions in another file where it's a request from an API, but the idea is the same here, I don't think that woould make a difference 通常,我会在另一个文件中调用这些Valdator电子邮件和密码功能,这是API的请求,但是这里的想法是相同的,我认为这不会有所作为

Your RegEx is never being executed because valueR.length == 0 will evaluate to false , which short-circuits your && before it can execute the second part. RegEx永远不会执行,因为valueR.length == 0计算结果为false ,这会在执行第二部分之前使&&短路。 Remember that if you are AND-ing two statements and the first statements evaluates to false , there is no need to evaluate the second statement because both false && true and false && false evaluate to false . 请记住,如果您对两个语句进行“与” false ,并且第一个语句的评估结果为false ,则无需评估第二个语句,因为false && truefalse && false评估为false

That being said, it makes no sense to check for valueR.length == 0 as a pre-condition for evaluating a RegEx on valueR - why would we run an RegEx on a 0-length string? 话虽这么说,将valueR.length == 0作为评估valueR上的RegEx的valueR是没有意义的-为什么我们要在长度为0的字符串上运行RegEx? You should flip this logic to !== . 您应该将此逻辑翻转为!==

And please always use === or !== in the future, as == gets messy with type conversion. 并请以后始终使用===!== ,因为==会导致类型转换混乱。

let validate_pass = valueR.length !== 0 && this.regex.password.test(valueR);

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

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