0

我需要使用 jquery validate 来验证 textarea。我正在寻找正则表达式来检查由空格分隔且长度超过 5 个字符的多个带引号的字符串..:

“引用的文本..” “一些其他引用的文本” “另一个引用的字符串” = 好

“引用的文本..” “” “另一个引用的字符串” = 不好

“引用文本..” “abcd” “另一个引用字符串” = 不好

以下仅检查第一个引用的文本...(“引用的字符串长于 5”“”-> 这通过但不应该)

$(document).ready(function()
{
   $.validator.addMethod("coll_regex", function(value, element) { 
   return this.optional(element) || /"(.*?)"/.test(value); 
    }, "Message here......");

$("#f_coll").validate(
{
    rules:{
    'coll_txt':{
        required: true,
        minlength: 5,
        maxlength: 200,
        coll_regex: true
        }
    },
    messages:{
    'coll_txt':{
        required: "Textarea is empty...",
        minlength: "Length must be, at least, 5 characters..",
        maxlength: "You exceeded the max_length !",
        coll_regex: "Use the quotes...."
       }
    },
    errorPlacement: function(error, element) {
      error.appendTo(element.next());
  }
});
});

有没有这样做的正则表达式???会很棒...谢谢

4

1 回答 1

2

您正在寻找的正则表达式是/^("[^\".]{5,}" )*"[^\".]{5,}"$/

'"abcdefg" "abcdefg" "01324"'.match(/^("[^\".]{5,}" )*"[^\".]{5,}"$/)  //--> true
'"abcdefg" "123" "01324"'.match(/^("[^\".]{5,}" )*"[^\".]{5,}"$/)  //--> false
'"abcdefg" "" "01324"'.match(/^("[^\".]{5,}" )*"[^\".]{5,}"$/)  //--> false

编辑:

这个更精确:/^("[^\".]{5,}"\s+)*"[^\".]{5,}"$/ 它允许组之间的任何空白,而不仅仅是一个空格。

于 2012-08-18T00:38:07.990 回答