0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

very good.. it works... however is there a way to have control on the max length (200) too in your regex?.. i think i will probably delete the maxlength method for my textarea validation 'cause it acts on the global length... i appreciate thank you!
Sure! just add your value vor maxLength right behind the {5,. It reads like {min,max}. so it is /^("[^\".]{5,200}" )*"[^\".]{5,200}"$/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.