简体   繁体   English

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

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

My goal is to check if an email input field (as a user is typing characters into it), matches a pattern or not.我的目标是检查 email 输入字段(当用户在其中输入字符时)是否与模式匹配。 This is my working example so far, but I feel stuck when it gets to the point of actually matching the process "while" typing...到目前为止,这是我的工作示例,但是当它到达实际匹配“同时”输入过程的地步时,我感到卡住了......

Below is my working example which I'm trying to capitalize on...下面是我正在尝试利用的工作示例......


   //...

    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?  
        }
      });
    }

    //...


Add a return value to your function. Yours as it is returns undefined which you can just change to return the result of re.test(email) .向您的 function 添加一个返回值。您的返回值undefined ,您可以更改它以返回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')
        }
    }
  });
//}

//...

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

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