Actions

Using regular expressions/fr: Difference between revisions

From LimeSurvey Manual

(Updating to match new version of source page)
(Updating to match new version of source page)
Line 2: Line 2:
__TOC__
__TOC__


 
Regular Expressions must start and finish with a forward slash ("/"). You can find a good library of regular expressions at http://www.regexlib.net/. These patterns will almost all work if surrounded with the forward slash.
<div class="simplebox">La traduction de cette page n'est pas terminée.</div>
 
__TOC__
 
=#Validation=
 
Lorsque vous définissez les attributs d'une question, vous devez indiquer son code, le texte de la question, le type de question,
 
le nombre minimum et/ou maximum de caractères, etc.
 
Cependant, il n'est pas possible définir tous les attributs de contrôle pouvant satisfaire les besoins des utilisateurs de LimeSurvey.
 
La zone de saisie ''Validation'' de l'écran d'édition d'une question joue ce rôle et permet d'ajouter des contrôles supplémentaires sur la réponse.
 
Ces contrôles se font gr&acirc;ce à des expressions régulières que l'on place dans la zone de saisie ''Validation''.
 
[[File:LimeSurvey-2011-06-01-19-18.png]]
 
Dans la situation présentée ci-dessus, la réponse sera considérée comme valide si d'une part elle est composée uniquement de lettres
 
en majuscule et d'autre part si le nombre de lettres est compris entre 1 et 10.
 
<div class="simplebox"><u>Attention</u>
 
Pour le contenu de la zone de saisie ''Validation'' soit évalué, il faut obligatoirement que l'utilisateur est saisi un caractère.
 
</div>
 
=#Bref rappel=
 
<div class="simplebox"><u>Avertissements</u>
 
L'objet de cette section n'est pas d'expliquer comment constituer des expressions régulières
 
ou bien d'expliquer leurs fonctionnements.
 
De très bons livres le font comme Maîtrise des expressions régulières de Jeffrey E. F. Friedl.
 
Il y a également des sites qui traitent de ces sujets comme  http://www.regxlib.net/ (en anglais)
 
ou bien http://perl.enstimac.fr/DocFr/perlrequick.html (en français).</div>
 
L'expression régulière est un moyen puissant,flexible et efficace d'appliquer un traitement sur du texte.
 
Gr&acirc;ce à une notation générale (motif, pattern), c'est une sorte de mini-langage de programmation qui permet de décrire
 
et d'analyser le texte. Plusieurs notations existent.
 
Celle utilisée dans LimeSurvey est du type PERL et doivent commencer et se terminer par le caractère slash ("/").
 
{|
|   Métacaractère||   Signification||
|-
|   .||   n'importe quel caractère sauf \n||
|-
|   <nowiki>[</nowiki>  ]|| tous les caractères énumérés  ||   <nowiki>[</nowiki>abc]
|-
|   <nowiki>[</nowiki>~94~  ]|| tous les caractères sauf ceux qui sont énumérés  ||   <nowiki>[</nowiki>~94~abc]
|-
|   ~124~ || tous les caractères sauf ceux qui sont énumérés  ||   mot1~124~mot2~124~mot3
|-
|   - || intervalle  ||   a-z
|-
|
|}
 
{|
|   Quantificateur||   Signification   || Exemple
|-
|   * ||   zéro ou plus  || A* = zéro ou plus de lettre A
|-
|   + || 1 ou plus       || A+ = une lettre A ou plus
|-
|   ? || 0 ou 1          || A? = pas de lettre A ou une seule lettre A
|-
| {n} || n fois exactement || A{3} = trois lettres A
|-
|{min,} || minimum min fois et plus || A{3,} trois lettres A ou plus
|-
|{min,max}|| minimum min et maximum max  fois || A{3,7} trois lettres A au minimum et au maximum 7 lettres A
|-
|
|}
 
{|
|   Caractères||   Signification || Équivalence
|-
| \n || nouvelle ligne||
|-
| \r || retour à la ligne||
|-
| \t || tabulation||
|-
| \v || tabulation vertical||
|-
| \f || nouvelle page||
|-
|   \s ||   caractères d'espacement|| <nowiki>[</nowiki>\n\r\t\f]
|-
|   \S || tous les caractères sauf les caractères d'espacement|| <nowiki>[</nowiki>~94~\n\r\t\f]
|-
|   \d || les chiffres||<nowiki>[</nowiki>0-9]
|-
| \D || tous les caractères sauf les chiffres||<nowiki>[</nowiki>~94~0-9]
|-
| \w || caractère constituant un mot||<nowiki>[</nowiki>0-9a-zA-Z_]
|-
| \W || tous les caractères ne constituant pas un mot||<nowiki>[</nowiki>~94~\w]
|-
| \xhh || caractère en hexadécimal(hh) ex: \x41 = A||
|-
|
|}
 
{|
|   Modificateur||   Signification
|-
| i || ignore la casse
|-
| x || le point reconnait aussi \n
|-
| m || mode multi lignes
|-
|
|}
 
Les modificateurs se mettent après le dernier slash ex : /<nowiki>[</nowiki>a-z]/i est équivalent à /<nowiki>[</nowiki>a-zA-Z]/
 
Vous pouvez tester vos expressions régulières sur http://www.spaweditor.com/scripts/regex/index.php
 
=#Quelques exemples=
 
<div class="simplebox">''Vous pouvez ajouter des expressions régulières mais testées auparavant!''</div>
 
Exemples (notez qu'elles sont toutes sur une seule ligne):
 
==Validation d'un code postal français==
 
<nowiki>/^0[1-9]|[1-8][0-9]|9[0-8]|2A|2B[0-9]{3}$/</nowiki>
 
Cette expression teste seulement de le format du code postal mais pas de sa validité.
 
==Validation du numéro de département français==
 
<nowiki>/^0[1-9]|[1-8][0-9]|9[0-8]|2A|2B$/</nowiki>
 
Cette expression teste seulement de le format du numéro du département mais pas de sa validité.
 
==Validation d'un nombre==
 
===Validation d'un nombre entier===
 
====nombre entier compris dans un intervalle défini (age, etc.) de 20 à 99====
 
<nowiki>/([2-9][0-9])/</nowiki>
 
====nombre entier compris dans un intervalle défini (age, etc.) de 18 à 35====
 
<nowiki>/(1[8-9]|2[0-9]|3[0-5])/</nowiki>
 
====nombre entier compris dans un intervalle défini (age, etc.) de 19 à 65====
 
<nowiki><div class="simplebox">(1[8-9]|[2-5][0-9]|6[0-5])$</nowiki>
 
====nombre entier compris dans un intervalle de 1 à 99999====
 
<nowiki>/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/</nowiki>  <nowiki>/^[1-9][0-9]{0,4}$/</nowiki>
 
C'est deux expressions produisent le même résultat.
 
====nombre entier compris dans plusieurs intervalles de 1 à 999, de 1.000 à 999.999====
 
<nowiki>/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,1}$/</nowiki>
 
====validation d'un score compris entre 1 et 10====
 
<nowiki>/^[1-9]{1}$|^10$/</nowiki>
 
====validation d'un score compris entre 1 et 100====
 
<nowiki>/^[1-9]?[0-9]{1}$|^100$/</nowiki>
 
===validation d'un nombre décimal===
 
Ce type de validation peut être utilisé dans le cas de prix, mesure
 
====validation d'un nombre décimal====
 
<nowiki>/^([1][0-9][0-9]|[1-9][0-9]|[0-9])[[\.)[0-9][0-9])?$/</nowiki>
 
les nombres entre ayant une partie décimale optionnelle à deux chiffres
 
  <nowiki>/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</nowiki>
 
===Monnaie===
 
====Euro (le signe Euro et les centimes sont optionnels)====
 
  <nowiki>/^\d+(\.(\d{3}]]?&euro;?$/</nowiki>
 
====Dollar US====
 
<nowiki>/^\$?\d+(\.(\d{2}))?$/</nowiki>
 
Le signe dollar en début de saisie et les cents sont optionnels.
 
====Prix en Franc Suisse====
 
C'est un nombre avec deux décimales  après la virgule dont le dernier chiffre est soit 5, soit 0.
 
<nowiki>/^(\d+)(\.\d(05)?)?$/</nowiki>
 
==Validation d'une date==
 
====validation d'une date au format jj/mm/aaaa====
 
<nowiki>/^\d{1,2}\/\d{1,2}\/\d{4}$/</nowiki>
 
====validation d'une date au format jj/mm/aaaa du 20 ou 21éme siècle====
 
<nowiki>/^\d{1,2}\/\d{1,2}\/(19|20)\d{2}$/</nowiki> a tester
 
====validation d'un mois (1-12)====
 
<nowiki>/^[0]?[1-9]$|^1[0-2]$/</nowiki>
 
Cette expression permet de saisir le mois de janvier soit 1, soit 01.
 
==Validation d'une heure==
 
<nowiki>/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/</nowiki>
 
cette expression permet de valider un format d'heure HH:MM ex : 11:32
 
<nowiki>/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/</nowiki>
 
Cette expression permet de valider un format d'heure HH:MM:SS où la partie de secondes est optionnelle ex : 11:32:53 ou bien 11:32
 
<nowiki>/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/</nowiki>
 
Cette expression permet de valider un format d'heure HH:MM:SS où la partie des secondes est obligatoire ex : 11:32:53
 
==Validation d'une longueur de saisie==
 
Pour certain type de question, LimeSurvey propose dans les paramètres avancés ce type de contrôle.
 
====validation d'une longueur de saisie minimum====
 
<nowiki>/^.{3,}$/</nowiki>
 
ici la longueur minimum est de 3 caractères quelconques
 
====validation d'une longueur de saisie maximum====
 
<nowiki>/^.{3,}$/</nowiki>
 
ici la longueur maximum est de 3 caractères quelconques
 
==Validation de texte==
 
Actuellement, le type de question ''Multiples zones de texte court'' ne permet pas de contrôler le minimum ou le maximum de réponses.
 
Une des manières d'y parvenir est d'utiliser le type question ''Zone de texte long'' sur laquelle on appliquera une expression régulière.
 
L'expression ci-dessous vérifie qu'il y a au moins un mot par ligne et qu'il y a au moins trois lignes mais pas plus de 10 lignes :
 
<nowiki>/(?:[</div>,.;:?!& \n\r]+(?: [<div class="simplebox">,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[</div>,.;:?!& \n\r]+(?: [<div class="simplebox">,.;:?!& \n\r]+)*)){2,10}/is</nowiki>
 
Si vous voulez vérifier qu'il y a au moins 5 mots par ligne, vous devez changer le premier et le dernier caractère étoile (*) par {4,}
 
<nowiki>/(?:[</div>,.;:?!& \n\r]+(?: [<div class="simplebox">,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[</div>,.;:?!& \n\r]+(?: [<div class="simplebox">,.;:?!& \n\r]+){4,})){2,10}/is</nowiki>
 
Si voulez 1 ou plusieurs mots par ligne et de 1 à 5 lignes, vous pouvez changer les valeurs du quantificateur {0,4}.
 
<nowiki>/(?:[</div>,.;:?!& \n\r]+(?: [<div class="simplebox">,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[</div>,.;:?!& \n\r]+(?: [<div class="simplebox">,.;:?!& \n\r]+)*)){0,4}/is</nowiki>
 
===Limiter le nombre de mots===
 
L'expression ci-dessous permet d'écrire au minimum 1 mot et au maximum 200 mots.
 
<nowiki>/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</nowiki>
 
Vous pouvez varier le minimum et le maximum selon vos besoins en changeant {0,199} ( voir ci-dessus le tableau ''Quantificateur'')
 
==Validation d'un mot==
 
==== mot dans une liste====
 
<nowiki>/^(arbre|feuille|fleur){1}$/</nowiki>
 
Le mot saisie doit correspondre exactement (minuscule) au mot dans la liste.
 
====validation du mot dans une liste====
 
<nowiki>/^(arbre|feuille|fleur){1}$/i</nowiki>
 
Ici, on ne tient pas compte des minuscules et des majuscules : Les mots suivants Arbre,arbre, ARBRE, ArBrE, etc. sont tous valides.


To test your regex you can [http://www.regexlib.net/RETester.aspx use this regex tester].
To test your regex you can [http://www.regexlib.net/RETester.aspx use this regex tester].
Line 307: Line 10:
Examples (note that these are all one line):
Examples (note that these are all one line):


=SPECIAL NOTE: Regular Expressions in Conditions=
=Important: Regular Expressions in conditions=


Note that when using regular expressions in the condition editor, do NOT include the beginning and ending slash.
Note that when using regular expressions in the condition editor, do NOT include the beginning and ending slash.
Line 313: Line 16:
=#Validation=
=#Validation=


<nowiki>/(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/</nowiki>
<source>
 
/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
The above regex will not validate email adresses with "+" - which is valid, and e.g. promoted by gmail. The below regex is slightly modified and allows "+":
</source>
 
<nowiki>/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/</nowiki>
 
It is still far from perfect - but I think its better then the original (also because start-of-string and end-of-string anchors are added). Mind. This will allow e.g. abc+++defg@somemail.office.com. Ideally the regex should be modified to only allow one "+" charachter.


=Postcodes:=
=Postcodes=


==Australian Postcodes:==
==Australian postcodes:==


  <nowiki>/^[0-9]{4}/</nowiki>
  <source>/^[0-9]{4}/</source>


==Brazilian Postcodes:==
==Brazilian postcodes==


  <nowiki>/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</nowiki>
  <source>/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</source>


==Canadian Postcodes:==
==Canadian postcodes==


  <nowiki>/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</nowiki>
  <source>/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</source>


==US Postal Codes:==
==US postal codes==


  <nowiki>/^[0-9]{5}([- /]?[0-9]{4})?$/</nowiki>
  <source>/^[0-9]{5}([- /]?[0-9]{4})?$/</source>


==UK Postcodes:==
==UK postcodes==


  <nowiki>/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</nowiki>
  <source>/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</source>


Note that this is not very exact, and a more exact validation is much more complex.  For example, see [http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive StackOverflow answer] and [http://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom#Regular_Expressions Wikipedia] for more information.
Note that this is not very exact, and a more exact validation is much more complex.  For example, see [http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive StackOverflow answer] and [http://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom#Regular_Expressions Wikipedia] for more information.


=Phone Numbers=
=Phone numbers=


==US Phone Number:==
==US phone numbers==


  <nowiki>/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/</nowiki>
  <source>/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/</source>


or
or


  <nowiki>/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/</nowiki>
  <source>/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/</source>


This second option will match all phone Canadian and US phone numbers that include non-digit symbols including
This second option will match all phone Canadian and US phone numbers that include non-digit symbols including


  <nowiki> . ( ) - (space)</nowiki>
  <source> . ( ) - (space)</source>


This will allow you to match phone numbers which resemble below.
This will allow you to match phone numbers which resemble below.
Line 367: Line 66:
*555555555
*555555555


==Australian Phone Number:==
==Australian phone numbers==


The following patterns match all various Australian mobile and landline phone numbers including with "+61" country prefix eg:
The following patterns match all various Australian mobile and landline phone numbers including with "+61" country prefix eg:
Line 383: Line 82:
Brackets, white space and hypens are ignored.
Brackets, white space and hypens are ignored.


The 'VERY PRECISE' versions listed here match against the first four or five digits in a number to ensure that they are valid Australian numbers.
The 'Very precise:' versions listed here match against the first four or five digits in a number to ensure that they are valid Australian numbers.


The 'NOT VERY PRECISE' only match against the first and second digit so may allow invaid numbers.
The 'Not very precise:' only match against the first and second digit so may allow invaid numbers.


===All Australian phone numbers (mobile and landline - area code required)===
===All Australian phone numbers (mobile and landline - area code required)===


VERY PRECISE
Very precise:
 
<source>/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/
</source>


<nowiki>/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/</nowiki>


NOT VERY PRECISE
Not very precise:


<nowiki>/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/</nowiki>
<source>/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/</source>


===All Australian phone numbers (landlines only - area code required)===
===All Australian phone numbers (landlines only - area code required)===


VERY PRECISE
Very precise:
 
<source>/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/</source>


<nowiki>/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/</nowiki>


NOT VERY PRECISE
Not very precise:


<nowiki>/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/</nowiki>
<source>/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/</source>


===New South Wales landline phone numbers (area code optional)===
===New South Wales landline phone numbers (area code optional)===


VERY PRECISE
Very precise:


<nowiki>/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/</nowiki>
<source>/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/</source>


NOT VERY PRECISE
Not very precise:


<nowiki>/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
<source>/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/</source>


===Victorian and Tasmanian landline phone numbers (area code optional)===
===Victorian and Tasmanian landline phone numbers (area code optional)===


VERY PRECISE
Very precise:


<nowiki>/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/<nowiki>
<source>/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/</source>


NOT VERY PRECISE
Not very precise:


<nowiki>/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
<source>/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/</source>


===Queensland landline phone numbers (area code optional)===
===Queensland landline phone numbers (area code optional)===


VERY PRECISE
Very precise:
 
<source>/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/</source>


<nowiki>/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/</nowiki>


NOT VERY PRECISE
Not very precise:


<nowiki>/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
<source>/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/</source>


===South Australia, Northern Territory, Western Australia landline phone numbers (area code optional)===
===South Australia, Northern Territory, Western Australia landline phone numbers (area code optional)===


VERY PRECISE
Very precise:


<nowiki>/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/</nowiki>
<source>/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/</source>


NOT VERY PRECISE
Not very precise:


<nowiki>/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
<source>/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/</source>


===Australian Mobile phone numbers only===
===Australian mobile phone numbers only===


VERY PRECISE
Very precise:


<nowiki>/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/</nowiki>
<source>/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/</source>


NOT VERY PRECISE


<nowiki>/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
Not very precise:
 
<source>/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/</source>


==Belgian phone number==
==Belgian phone number==
  <nowiki>/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</nowiki>
  <source>/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</source>
==Belgian mobile phone number==
==Belgian mobile phone number==
  <nowiki>/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</nowiki>
  <source>/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
==French phone number==
==French phone number==
  <nowiki>/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/</nowiki>
  <source>/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/</source>
==French mobile phone number==
==French mobile phone number==
  <nowiki>/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/</nowiki>
  <source>/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/</source>
==Luxemburg phone number==
==Luxemburg phone number==
  <nowiki>/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</nowiki>
  <source>/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
==Luxemburg mobile phone number==
==Luxemburg mobile phone number==
  <nowiki>/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/</nowiki>
  <source>/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/</source>
=German marks (with optional plus or minus)=
=German marks (with optional plus or minus)=
  <nowiki>/^[1-6]{1}[\+|\-]?$/</nowiki>
  <source>/^[1-6]{1}[\+|\-]?$/</source>
=Age Validation=
=Age validation=
===Example: Age 20-99===
Example: Age 20-99
  <nowiki>/([2-9][0-9])/</nowiki>
  <source>/([2-9][0-9])/</source>
===Example: Age 18-35===
 
  <nowiki>/(1[8-9]|2[0-9]|3[0-5])/</nowiki>
 
===Example: Age 19-65===
Example: Age 18-35
  <nowiki>^(1[8-9]|[2-5][0-9]|6[0-5])$</nowiki>
  <source>/(1[8-9]|2[0-9]|3[0-5])/</source>
 
 
Example: Age 19-65
  <source>^(1[8-9]|[2-5][0-9]|6[0-5])$</source>
 
=Number validation=
=Number validation=
==Numbers from 1 to 99999==
==Numbers from 1 to 99999==
  <nowiki>/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/</nowiki>
  <source>/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/</source>
  <nowiki>/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster</nowiki>
  <source>/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster</source>
==Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999==
==Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999==
  <nowiki>/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/
  <source>/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/</source>
accepts numbers from 1 to 999, 1.000 to 999.999 to 999.999.999 but
Accepts numbers from 1 to 999, 1.000 to 999.999 to 999.999.999 but
rejects numbers like 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333</nowiki>
rejects numbers like 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333
 
==Number validation with optional decimal (for price)==
==Number validation with optional decimal (for price)==
  <nowiki>/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/ accepts numbers from 0 to 199, with 2 decimal optional</nowiki>
Accepts numbers from 0 to 199, with 2 decimal optional:
<nowiki>
  <source>/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/</source>
/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/
 
forces two decimal points
 
accepts numbers from 1.00 to 999,999,999.00 with an optional comma delimiting thousands/millions
Forces two decimal points and accepts numbers from 1.00 to 999,999,999.00 with an optional comma delimiting thousands/millions
including all of the following: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00
including all of the following: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00
but not 1,23.00, 12,3.4 or 1234,43.04</nowiki>
but not 1,23.00, 12,3.4 or 1234,43.04
  <nowiki>
  <source>/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ same as above but the two decimal points are optional</nowiki>
 
Same as above but the two decimal points are optional:
<source>/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ </source>


==Month (1-12)==
==Month (1-12)==
Line 501: Line 214:
If you want to ask for the month a person was born you can validate the input as follows:
If you want to ask for the month a person was born you can validate the input as follows:


  <nowiki>/^[0]*[1-9]$|^[0]*1[0-2]$/</nowiki>
  <source>/^[0]*[1-9]$|^[0]*1[0-2]$/</source>


=Minimum width (set to 3 in this example)=
=Minimum width (set to 3 in this example)=


  <nowiki>/^.{3,}$/</nowiki>
  <source>/^.{3,}$/</source>


=Currency=
=Currency=
Line 511: Line 224:
==US currency (dollar sign and cents optional)==
==US currency (dollar sign and cents optional)==


  <nowiki>/^\$?\d+(\.(\d{2}]]?$/</nowiki>
  <source>/^\$?\d+(\.(\d{2}]]?$/</source>


==Swiss price==
==Swiss price==
Line 517: Line 230:
A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:
A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:


  <nowiki>/^(\d+)(\.\d(05)?)?$/</nowiki>
  <source>/^(\d+)(\.\d(05)?)?$/</source>


=Validate score=
=Validate score=
Line 523: Line 236:
==1-10==
==1-10==


  <nowiki>/^[1-9]{1}$|^10$/</nowiki>
  <source>/^[1-9]{1}$|^10$/</source>


==1-100==
==1-100==


  <nowiki>/^[1-9]?[0-9]{1}$|^100$/</nowiki>
  <source>/^[1-9]?[0-9]{1}$|^100$/</source>


=Text validation=
=Text validation=
Line 535: Line 248:
The following test for at least one word per line for at least 3 lines and no more than 10 lines.
The following test for at least one word per line for at least 3 lines and no more than 10 lines.


  <nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is</nowiki>
  <source>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is</source>


If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.
If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.


  <nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is</nowiki>
  <source>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is</source>


If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to '''0,4''' (note you use 0 because you're already matching the first line).
If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to '''0,4''' (note you use 0 because you're already matching the first line).


  <nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is</nowiki>
  <source>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is</source>


==Word count==
==Word count==


  <nowiki>The following restricts the number of words allowed to a minimum of 1 and a maximum of 200
  <source>The following restricts the number of words allowed to a minimum of 1 and a maximum of 200


/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</nowiki>
/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</source>


  <nowiki>To increase the minimum change the zero part of {0,199}
  <source>To increase the minimum change the zero part of {0,199}


To increase or decrease the maximum change the "199" part of {0,199}</nowiki>
To increase or decrease the maximum change the "199" part of {0,199}</source>


=Time validation=
=Time validation=


There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Currently  (2009/04/06) LimeSurvey (v1.80plus) doesn't have a Time queston type so instead you can use "short free text" with one of the nine validation regular expressions below:
There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Although it is an option to use the date question type (it can also capture time) you can use "short free text" with one of the validation regular expressions below:
 
The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.
 
<source>/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/</source>
 
<source>/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/</source>
 
<source>/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/</source>
 
 


<nowiki>The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.
The following three match 12 hour time, as above with seconds, optional seconds and with seconds required


/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/
<source>/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/</source>


/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/
<source>/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/</source>


/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/~/pp<span style="color:pp~The following three match 12 hour time, as above with seconds, optional seconds and with seconds required
<source>/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/</source>


/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/


/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/


/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/~/pp</span>pp~The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required
The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required


/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/
<source>/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/</source>


/^(?:(?:00:[0-5][0-9](?<center>[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?</center>[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?<center>[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?</center>[0-5][0-9])?)$/
<source>/^(?:(?:00:[0-5][0-9](?[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?[0-5][0-9])?)$/</source>


/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/</nowiki>
<source>/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/</source>


=US States=
=US states=


To validate for one state use the following (example is Texas):
To validate for one state use the following (example is Texas):
*TX uppercase only = <nowiki>/^(TX)$/</nowiki>
*TX uppercase only = <source>/^(TX)$/</source>
*tx lowercase only = <nowiki>/^(tx)$/</nowiki>
*tx lowercase only = <source>/^(tx)$/</source>
*TX upper or lowercase = <nowiki>/^([T|t][X|x])$/</nowiki>
*TX upper or lowercase = <source>/^([T|t][X|x])$/</source>


=Profanity Filter=
=Profanity Filter=
Line 590: Line 311:
To filter profanity words from an answer:
To filter profanity words from an answer:


  <nowiki>/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</nowiki>
  <source>/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</source>


Replace "ENTERPROFANITYHERE" with your bad word.
Replace "ENTERPROFANITYHERE" with your bad word.


The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.
The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.

Revision as of 18:13, 7 July 2016

Regular Expressions must start and finish with a forward slash ("/"). You can find a good library of regular expressions at http://www.regexlib.net/. These patterns will almost all work if surrounded with the forward slash.

To test your regex you can use this regex tester.

Please add successfully tested regular expressions!

Examples (note that these are all one line):

Important: Regular Expressions in conditions

Note that when using regular expressions in the condition editor, do NOT include the beginning and ending slash.

#Validation

/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/

Postcodes

Australian postcodes:

/^[0-9]{4}/

Brazilian postcodes

/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/

Canadian postcodes

/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/

US postal codes

/^[0-9]{5}([- /]?[0-9]{4})?$/

UK postcodes

/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i

Note that this is not very exact, and a more exact validation is much more complex.  For example, see StackOverflow answer and Wikipedia for more information.

Phone numbers

US phone numbers

/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/

or

/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/

This second option will match all phone Canadian and US phone numbers that include non-digit symbols including

 . ( ) - (space)

This will allow you to match phone numbers which resemble below.

  • (555)555 5555
  • 555.555.5555
  • 555 555 5555
  • (555)-555-5555
  • 555-555-5555
  • 555555555

Australian phone numbers

The following patterns match all various Australian mobile and landline phone numbers including with "+61" country prefix eg:

  • (02) 9123 6535
  • 03 1234-5345
  • 0412 345 678
  • +61 2 3456 789

But not:

  • 234 3450 234
  • a234 534 3432
  • 134567
  • 123456789013

Brackets, white space and hypens are ignored.

The 'Very precise:' versions listed here match against the first four or five digits in a number to ensure that they are valid Australian numbers.

The 'Not very precise:' only match against the first and second digit so may allow invaid numbers.

All Australian phone numbers (mobile and landline - area code required)

Very precise:

/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/


Not very precise:

/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/

All Australian phone numbers (landlines only - area code required)

Very precise:

/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/


Not very precise:

/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/

New South Wales landline phone numbers (area code optional)

Very precise:

/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/

Not very precise:

/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/

Victorian and Tasmanian landline phone numbers (area code optional)

Very precise:

/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/

Not very precise:

/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/

Queensland landline phone numbers (area code optional)

Very precise:

/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/


Not very precise:

/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/

South Australia, Northern Territory, Western Australia landline phone numbers (area code optional)

Very precise:

/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/

Not very precise:

/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/

Australian mobile phone numbers only

Very precise:

/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/


Not very precise:

/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/

Belgian phone number

/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/

Belgian mobile phone number

/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/

French phone number

/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/

French mobile phone number

/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/

Luxemburg phone number

/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/

Luxemburg mobile phone number

/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/

German marks (with optional plus or minus)

/^[1-6]{1}[\+|\-]?$/

Age validation

Example: Age 20-99

/([2-9][0-9])/


Example: Age 18-35

/(1[8-9]|2[0-9]|3[0-5])/


Example: Age 19-65

^(1[8-9]|[2-5][0-9]|6[0-5])$

Number validation

Numbers from 1 to 99999

/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/
/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster

Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999

/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/

Accepts numbers from 1 to 999, 1.000 to 999.999 to 999.999.999 but rejects numbers like 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333

Number validation with optional decimal (for price)

Accepts numbers from 0 to 199, with 2 decimal optional:

/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/


Forces two decimal points and accepts numbers from 1.00 to 999,999,999.00 with an optional comma delimiting thousands/millions including all of the following: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00 but not 1,23.00, 12,3.4 or 1234,43.04

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/


Same as above but the two decimal points are optional:

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/

Month (1-12)

If you want to ask for the month a person was born you can validate the input as follows:

/^[0]*[1-9]$|^[0]*1[0-2]$/

Minimum width (set to 3 in this example)

/^.{3,}$/

Currency

US currency (dollar sign and cents optional)

/^\$?\d+(\.(\d{2}]]?$/

Swiss price

A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:

/^(\d+)(\.\d(05)?)?$/

Validate score

1-10

/^[1-9]{1}$|^10$/

1-100

/^[1-9]?[0-9]{1}$|^100$/

Text validation

currently multiple short text doesn't support minimum or maximum answers. One way around this is to use a long free text type question with a regular expression.

The following test for at least one word per line for at least 3 lines and no more than 10 lines.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is

If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is

If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to 0,4 (note you use 0 because you're already matching the first line).

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is

Word count

The following restricts the number of words allowed to a minimum of 1 and a maximum of 200

/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/
To increase the minimum change the zero part of {0,199}

To increase or decrease the maximum change the "199" part of {0,199}

Time validation

There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Although it is an option to use the date question type (it can also capture time) you can use "short free text" with one of the validation regular expressions below:

The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.

/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/


The following three match 12 hour time, as above with seconds, optional seconds and with seconds required

/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/
/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/
/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/


The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required

/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/
/^(?:(?:00:[0-5][0-9](?[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?[0-5][0-9])?)$/
/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/

US states

To validate for one state use the following (example is Texas):

  • TX uppercase only =
    /^(TX)$/
  • tx lowercase only =
    /^(tx)$/
  • TX upper or lowercase =
    /^([T|t][X|x])$/

Profanity Filter

To filter profanity words from an answer:

/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/

Replace "ENTERPROFANITYHERE" with your bad word.

The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.