views:

54

answers:

2

I have a "description textarea" inside a form where user may enter a description for an item.

This is validated with javascript before the form beeing submitted.

One of the validation-steps is this:

  else if (!fld.value.match(desExp)){

And desExp:

 var desExp = /^\s*(\w[^\w]*){3}.*$/gm;

Now my problem, this works fine on all cases except for descriptions where the description BEGINS with a special character of the swedish language (å, ä, ö).

This wont work:

    åäö hello world

But this will:

    hello world åäö

Any fixes?

Thanks

+2  A: 

I'd imagine using a broader category would work, something like:

/^\s*([\wåäö][^\w]*){3}.*$/gm;
Jakub Hampl
Thanks... this works!
Camran
+1  A: 

Unless I'm misunderstanding, you're just looking to verify that the string has at least 3 word characters? \s* at the beginning tells it to look for as many white-space characters as possible. Then the pattern (\w[^\w]) will match any word character followed by zero or more non-word characters. The {3} will assert that there are three word characters. The . at the end will match the rest of the string.

So, if my understanding and breakdown is correct, then what you would want is this, right?

/(\w\W*){3}/gm

It will match any string that has 3 word characters, no matter what's inside of them. It's slightly different than yours, as yours requires nothing but whitespace before the first word character. I'm not sure if that's what you intended or not, but if it is, just let me know and I'll modify it...

Edit Actually, I found this question which is related:

So taking that into account, you could do something like this:

var desExp = /^\s*([\w\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][^\w\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*){3}.*$/gm;

The block [\w\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF] will match any word character (either UTF8 or Latin-1).

ircmaxell