# Creating New Validation Patterns

## Usage

Validation patterns can be applied to questions on the validation tab of the question. Patterns can be added to that drop down by being added on the pattern tab of the Configuration Manager. Validation patterns are built using regular expressions. Here are some of the symbols/terminology used to put together regular expressions.

Anchors | Classes | Quantifiers | Groups & Ranges | Special Characters | |||||

^ | Start of string | \s | White space | * | 0 or more | . | Any character except new line (\n) | \n | New line |

$ | End of string | \S | Not white space | + | 1 or more | (a|b) | a or b | \t | Tab |

\d | Digit | ? | 0 or 1 | [abc] | Range (a or b or c) | \r | Carriage return | ||

\D | Not digit | {3} | Exactly 3 | [^abc] | Not a or b or c | ||||

{3,} | 3 or more | [a-q] | Letter from a to q | ||||||

{3,5} | 3,4, or 5 |

## Currency Pattern Example

Checking that a number is a two digit decimal (good for currancy) is the equivalent checking that there are any number of numbers, ranging from 0 to 9, before a decimal point and exactly two digits, also ranging from 0 to 9, after the decimal point.

Start | Any Number of digits (0-9) | decimal point (needs to be escaped) | Exactly two digits (0-9) | End |

^ | \d* | \. | \d{2} | $ |

So the resulting pattern would be: ^\d*\.\d{2}$

Note that this would be without a currency symbol (like a dollar sign) and without commas. This would not accept $1.00 or 1,000.00, for example. Say you wanted to allow them to enter a $ or commas in the number. Lets model the number before the decimal point first. If someone uses commas, there can be between 1 and 3 leading digits and there must 3 digits between commas. If they don't use commas, there can be any number of digits.

One to three digits | a comma and then three digits repeated any number of times | Any number of digits |

\d{1,3} | (\,\d{3})* | \d* |

So the pattern for the number before the decimal would be: (\d{1,3}(\,\d{3})*|(\d*))

Start | Possible Dollar Sign | Pattern for the number before the decimal | decimal point | Exactly two digits (0-9) | End |

^ | \$? | (\d{1,3}(\,\d{3})*|(\d*)) | \. | \d{2} | $ |

So the resulting pattern would be: ^\$?(\d{1,3}(\,\d{3})*|(\d*))\.\d{2}$

## IP Address Example

IP addresses appear simple, but when you get down to modeling them, they are more complicated than they appear. They are 4 strings of at least one digit, maximum of three separated by a decimal. The minimum for the first number is 1, the minimum for the other numbers are 0. The maximum for each number is 255. These ranges need to be built. The three 0-255 sets are easier to put together because they start at zero. Let's break the 255-0 range down into pieces we can model.

255-250 | 249-200 | 199-0 |

2 followed by 5 followed by any digit from 0 to 5 | 2 followed by any digit from 0 to 4 followed by any digit 0 to 9 | One digit from 0 to 9 possibly proceeded by a 1 and possibly followed by any digit from 0 to 9 |

25[0-5] | 2[0-4][0-9] OR 2[0-4]\d | 1?[0-9][0-9]? OR 1?\d\d? |

So the resulting group is (25[0-5]|2[0-4][0-9]|[1]?[0-9][0-9]?)

For the next group, the first 255 of the IP, zero isn't really a valid option, so the grouping is a little more complex.

255-250 | 249-200 | 199-100 | 99-1 |

2 followed by 5 followed by any digit from 0 to 5 | 2 followed by any digit from 0 to 4 followed by any digit 0 to 9 | 1 followed by any digit from 0 to 9 followed by any digit from 0 to 9 | Any digit from 1 o 9 possibly followed by any digit from 0 to 9 |

25[0-5] | 2[0-4][0-9] OR 2[0-4]\d | 1[0-9][0-9] OR 1\d{2} | [1-9][0-9]? |

So the resulting group is (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?)

Now to put these together into an IP address.

Start | First Group | decimal | Other Groups | End |

^ | (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?) | \. | (25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?) | $ |

So the resulting pattern would be: ^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)$