Jquery值匹配正则表达式 [英] Jquery Value match Regex

查看:134
本文介绍了Jquery值匹配正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过JQuery验证电子邮件的输入:

I am trying to validate the input for E-Mail via JQuery:

我的JQuery

<script>
/* <![CDATA[ */
  jQuery(function(){
   $( ".mail" ).keyup(function() {
   var VAL = $(this).val();
   var email = new RegExp(^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$);

   if(VAL.test(email)){
   alert('Great, you entered an E-Mail-address');
   }
   });
  });
  /* ]]> */
  </script>

即使我输入了example@example.com,也不会发出警报。
我已经尝试过.test()和.match(),我做错了什么?

This won't alert even though I entered example@example.com. I already tried .test() and .match(), what did I do wrong?

推荐答案


  • 将字符串传递给RegExp或使用 // 语法创建正则表达式

  • 调用 regex.test(string),而不是 string.test(正则表达式)

    • Pass a string to RegExp or create a regex using the // syntax
    • Call regex.test(string), not string.test(regex)
    • 所以

      jQuery(function () {
          $(".mail").keyup(function () {
              var VAL = this.value;
      
              var email = new RegExp('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$');
      
              if (email.test(VAL)) {
                  alert('Great, you entered an E-Mail-address');
              }
          });
      });
      

      这篇关于Jquery值匹配正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆