使用Twitter Bootstrap Popover修复jquery验证插件中的regex [英] Fix regex in jquery validation plugin with twitter bootstrap popover

查看:128
本文介绍了使用Twitter Bootstrap Popover修复jquery验证插件中的regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,当有空格时会显示一个弹出框.

I have this code which shows a popover when there is a white space.

$.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        });

 $('#validateForm').validate({
     rules: {
         product: {
             required: true,
             term: {regex: /^\s*$/}
         }
     },
     messages: {
         product: {
             required: "A text is much",
             term: "Please avoid spaces"
         },
     },  

     showErrors: function (errorMap, errorList) {

         $.each(this.successList, function (index, value) {
             $('#'+value.id+'').popover('destroy');
         });


         $.each(errorList, function (index, value) {

             $('#'+value.element.id+'').attr('data-content',value.message).popover({
                 placement: 'top',
                 trigger: 'manual'
             }).popover('show');

         });

     }

 });

发生的事情是,一旦没有空格,弹出框就不会被销毁.我在做什么错了?

What happens is, the popover doesnt get destroyed once there is no whitespace. What am i doing wrong?

推荐答案

此正则表达式对于空格和空字符串均有效.

This regular expression is valid for both white-space and for an empty string.

如果只想匹配空格,则应使用此正则表达式:/^\s+$/

If you want to match only white-space, you should use this regex: /^\s+$/

这篇关于使用Twitter Bootstrap Popover修复jquery验证插件中的regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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