24

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

9 Answers 9

37
/^[a-zàâçéèêëîïôûùüÿñæœ .-]*$/i

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

6
  • 4
    Strictly speaking ñ is not french, but the OP did not explicitely exclude non-french characters.
    – mouviciel
    Dec 17, 2009 at 14:36
  • 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.
    – apaderno
    Dec 17, 2009 at 14:39
  • 2
    @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
    Dec 17, 2009 at 15:08
  • 1
    @Amber, I think the u pattern modifier is appropriate here, n'est-ce pas? Feb 7, 2020 at 8:34
  • The problem with this approach is with newspaper headlines like « Björn Borg roi de Paris!»
    – PatrickT
    Jun 8, 2020 at 15:47
28

Simplified solution:

/^[a-zA-ZÀ-ÿ-. ]*$/

Explanation:

^ Start of the string [ ... ]* Zero or more of the following: a-z lowercase alphabets A-Z Uppercase alphabets À-ÿ Accepts lowercase and uppercase characters including letters with an umlaut - dashes . periods spaces $ End of the string

2
  • 9
    I think À-ÿ is wrong. Shouldn't it be À-Ÿ (example)? Aug 29, 2019 at 9:53
  • Yes, you're right. Feb 18, 2022 at 9:02
8

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
0
3

The character class I've been using is the following:

[\wÀ-Üà-øoù-ÿŒœ]. This covers a slightly larger character set than only French, but excludes a large portion of Eastern European and Scandinavian diacriticals and letters that are not relevant to French. I find this a decent compromise between brevity and exclusivity.

To match/validate complete sentences, I use this expression: [\w\s.,!?:;&#%’'"()«»À-Üà-øoù-ÿŒœ], which includes punctuation and French style quotation marks.

1

Simply use the following code :

     /[\u00C0-\u017F]/
0

This line of regex pass throug all of cirano de bergerac french text: (you will need to remove markup language characters http://www.gutenberg.org/files/1256/1256-8.txt

^([0-9A-Za-z\u00C0-\u017F\ ,.\;'\-()\s\:\!\?\"])+
1
  • You're escaping a bunch of stuff that don't need to be escaped within a character class
    – Tom Auger
    Feb 22, 2019 at 14:40
0

All French and Spanish accents /^[a-zA-ZàâäæáãåāèéêëęėēîïīįíìôōøõóòöœùûüūúÿçćčńñÀÂÄÆÁÃÅĀÈÉÊËĘĖĒÎÏĪĮÍÌÔŌØÕÓÒÖŒÙÛÜŪÚŸÇĆČŃÑ .-]*$/

-2

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.

0
-3

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.