lollo
lollo

Reputation: 165

jquery validate textarea

I need to validate a textarea with jquery validate. I'm looking for regex to check multiple quoted strings separated by a whitespace and longer more than 5 chars.. :

"quoted text.." "some other quoted text" "another quoted string" = good

"quoted text.." "" "another quoted string" = not good

"quoted text.." "abcd" "another quoted string" = not good

The following checks only the first quoted text ... ("quoted string longer than 5" "" --> this passes but it shouldn't)

$(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());
  }
});
});

is there a regex that does this ??? Would be great ... Thank you

Upvotes: 0

Views: 3278

Answers (1)

LeJared
LeJared

Reputation: 2052

The regex you're looking for is /^("[^\".]{5,}" )*"[^\".]{5,}"$/

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

EDIT:

This one is more precise: /^("[^\".]{5,}"\s+)*"[^\".]{5,}"$/ It allows any whitespace between groups, not only a single space.

Upvotes: 2

Related Questions