tags:

views:

164

answers:

6

Hi,

I need a function or a regular expression to validate strings which contain alpha characters (including French ones), minus sign (-), dot (.) and space (excluding everything else)

Thanks

A: 

This might suit:

/^[ a-zA-Z\xBF-\xFF\.-]+$/

It lets a few extra chars in, like ÷, but it handles quite a few of the accented characters.

nickf
woo! -1 and accepted!
nickf
+1  A: 
/^[a-zàâçéèêëîïôûùüÿñæœ .-]*$/i

Use of /i for case-insensitivity to make things simpler. If you don't want to allow empty strings, change * to +.

Amber
Strictly speaking ñ is not french, but the OP did not explicitely exclude non-french characters.
mouviciel
French doesn't use some of the characters the regular expression is trying to match, and the regular expression is not generic enough if it wants to match all those characters that are used in languages like French.
kiamlaluno
@kiamluno, except for ñ french uses all other mentioned characters, some in only few words, like "où" (where) or "L'Haÿ-les-Roses" (a city near Paris)
mouviciel
or "Je suis un Piñata"
nickf
A: 

[\w .-] should suffice, but you'll need to have \w consider the locale and/or put it into Unicode mode, so \w matches what Unicode defines as alpha-numeric characters. How to do that in PHP is probably just a Google away.

Alex Brasetvik
\w is actually better than /[A-Za-z]/u nice.....
Pragati Sureka
+1  A: 

Try:

/^[\p{L}-. ]*$/u

This says:

^         Start of the string
[ ... ]*  Zero or more of the following:
  \p{L}     Unicode letter characters
  -         dashes
  .         periods
            spaces
$         End of the string
/u        Enable Unicode mode in PHP
John Feminella
A: 

/[A-Za-z-\.\s]/u should work.. /u switch is for UTF-8 encoding

Pragati Sureka
A: 

Thanks a lot for all your answers.

nextu