如何检查输入是否与 jQuery 的字符串模式匹配

How to check if on input matches a string pattern with jQuery

我的目标是检查电子邮件输入字段(当用户在其中输入字符时)是否与模式匹配。到目前为止,这是我的工作示例,但是当它到达实际匹配“同时”输入过程的地步时,我感到卡住了...

下面是我正在尝试利用的工作示例...


   //...

    var $attemail = mtarget.find(".my-email-field"); //INPUT

    function validateEmail(email) {
      var re = /\S+@\S+\.\S+/;
      if (re.test(email) == false) {
        console.log('Email field is not valid');
        //issueFlag.push(noticeEmail);
      }
    }

    function checkEmailOnType() {
      $attemail.on("input", function(){
        var $characters = jQuer(this).val();

        if ($characters.toLowerCase())  {
            //IF MY CHARACTER TYPING IS PASSING OR NOT PASSING 
            //MY validateEmail() FUNCTION ABOVE, HOW DO WE SAY THAT?  
        }
      });
    }

    //...


为您的函数添加一个 return value。你的 returns undefined 你可以将 return 更改为 re.test(email).

的结果
//...

var $attemail = mtarget.find(".my-email-field"); //INPUT

function validateEmail(email) {
  var re = /\S+@\S+\.\S+/;
  return re.test(email)
}

//function checkEmailOnType() {
  $attemail.on("input", function(){
    var $characters = jQuer(this).val();

    if ($characters.toLowerCase())  {
        //IF MY CHARACTER TYPING IS PASSING OR NOT PASSING 
        //MY validateEmail() FUNCTION ABOVE, HOW DO WE SAY THAT?
        if(validateEmail($characters) == false){
            console.log('Email field is not valid')
        }
    }
  });
//}

//...