Validating Common Form Input - Part 8

Published on 3rd of October 2008. Copyright Tavs Dokkedahl. Displayed 1691 time(s)

Table of regexes

Below is a summary of the regular expressions we have used in the tutorial.

Username
Only lowercase letters. Length from 0 to 16.

/^[a-z]{0,16}$/
Case-insensitive. Length from 8 to 16.

/^[a-z]{8,16}$/i
Case-insensitive and digits. Length from 8 to 16.

/^[a-z0-9]{8,16}$/i or
/^[a-z\d]{8,16}$/i
Case-insensitive and digits ignoring leading and trailing spaces. Length from 8 to 16.

/^\s*[a-z\d]{8,16}\s*$/i
Names
A single name, case-insensitive, ignoring leading and trailing spaces. Length 4 to 32.

/^\s*[a-z]{4,32}\s*$/i
A single name, case-insensitive, including unicode characters, ignoring leading and trailing spaces. Length 4 to 32.

/^\s*[a-z\xe6\xf8\xe5]{4,32}\s*$/i
A single name, case-insensitive, national characters using UTF-8, ignoring leading and trailing spaces. Length 4 to 32.

/^\s*[a-zæøå]{4,32}\s*$/i
Multiple names, case-insensitive, ignoring leading and trailing spaces. Length 4 to 32.
Note: will validate strings consisting only of spaces.

/^\s*[a-z\s]{4,32}\s*$/i
Multiple names (at least 1), case-insensitive, ignoring leading and trailing spaces. Each name at least 1 characters. Abbreviations and names with hyphens allowed.

/^\s*[a-z]+([a-z]*\.|\s*\-\s*[a-z]+)?(\s+[a-z]+(\.|\s*\-\s*[a-z]+)?)*\s*$/i;
Phone numbers
A single number, no spaces, ignoring leading and trailing spaces. Length is 8.

/^\s*\d{8}\s*$/
A single number, no spaces, ignoring leading and trailing spaces. Length is at least 7.

/^\s*\d{7,}\s*$/
Any number of digits, any number of space. Length is at least 1.

/^(\s*\d+\s*)+$/
Passwords
At least 1 lowercase letter.

/[a-z]+/
At least 1 uppercase letter.

/[A-Z]+/
At least 1 digit.

/\d+/
At least 1 special character including underscore.

/[\W_]+/
No spaces. Will return true if a space is found.

/\s/
Website URL
www. followed by any allowed character 1 through 255 times ending in .com, ignoring leading and trailing spaces.

/^\s*www\.[a-z\d\-]{1,255}\.com\s*$/
www. followed by any allowed character 1 through 255 times ending in any name which is 2 through 6 letters long, ignoring leading and trailing spaces.

/^\s*www\.[a-z\d\-]{1,255}\.[a-z]{2,6}\s*$/
Any number of subdomains (length 1 to 63), followed by domain name (length 1 to 255), followed by top level domain (length 2 to 6), ignoring leading and trailing spaces.

/^\s*([a-z\d\-]{1,63}]\.)*[a-z\d\-]{1,255}\.[a-z]{2,6}\s*$/
Optional http://, followed by any number of subdomains (length 1 to 63), followed by domain name (length 1 to 255), followed by top level domain (length 2 to 6), ignoring leading and trailing spaces.

/^\s*(http\:\/\/)?([a-z\d\-]{1,63}]\.)*[a-z\d\-]{1,255}\.[a-z]{2,6}\s*$/
Email addresses
Address consisting of a - z, digits and underscore (length 1 to 255), followed by @ and simple domain name, ignoring leading and trailing spaces.

/^\s*[a-z\d_]{1,255}@[a-z\d\-]{1,255}\.[a-z]{2,6}\s*$/
Address consisting of a - z, digits and underscore and dot (length 1 to 255), followed by @ and simple domain name, ignoring leading and trailing spaces.

/^\s*[a-z\d_]+(\.[a-z\d_]+)*@[a-z\d\-]{1,255}\.[a-z]{2,6}\s*$/
« Part 7  

Leave a comment

Name

Email (if you want a response)

Comment (no HTML)

Spam challenge
Sorry to bother you but spam is a royal pain, so please answer this simple question to verify that you are in fact human(oid)

Question: "What is the 3 letter acronym for Cascading Style Sheets?"

Answer:

1 comment(s)