Created
June 28, 2011 02:02
-
-
Save pinktrink/1050335 to your computer and use it in GitHub Desktop.
Validation concept for Contact-Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//This is what I'm considering for validation. It should work both in JavaScript and PHP, and should only have to be written out in the configuration. | |
$validate = array( | |
'first_name' => 'filled|name|max:64', //Max size of the field can only be 64 | |
'last_name' => 'required|equals:[Jones, James, Smith]|max:64', //'required' is the same as 'filled', and this will only validate if the last name is Jones, James, or Smith. | |
'middle_name' => 'optional|equals:Danger', //'optional' itself is optional. This will only validate if the middle name is Danger. | |
'Title' => 'optional|truncate:5' //This will always validate, but if any value is entered that is longer than 5 characters, it will truncate it down to 5 characters. | |
); | |
/*Obviously there will be a lot more in there as I go along, what I've got right now are: | |
trim - removes whitespace from the beginning and end of the data, should be placed at the beginning. | |
rtrim - removes whitespace from the end of the data, should be placed at the beginning. | |
ltrim - removes whitespace from the beginning of the data, should be placed at the beginning. | |
numeric - validates only if the value is numeric (this includes 1.1 and so forth, and scientific notation) | |
filled, required - validates only if the field has something in it. | |
empty, blank - validates only if the field is left blank. | |
optional - always validates. Putting this with others (beyond formatting directives) will essentially make the field required. Added in for ostensibility. | |
min:<number> - validates only if the string's character count is over <number>. | |
max:<number> - validates only if the string's character count is under <number>. | |
size:<number>|[<set>] - validates only if the string's character count is <number> or one of <set> | |
truncate:<number> - truncates the string to <number> characters. | |
equal:<value>|[<set>] - validates only if the string is equal to <value> or one of <set> | |
notequal:<value>|[<set>] - validates only if the string is not equal to <value> or one of <set> | |
>:<number>, gt:<number> - validates only if the value is greater than <number>. Best paired with numeric. | |
>=:<number>, gte:<number> - validates only if the value is greater than or equal to <number>. Best paired with numeric. | |
<:<number>, lt:<number> - validates only if the string is less than <number>. Best paired with numeric. | |
<=:<number>, lte:<number> - validates only if the string is less than or equal to <number>. Best paired with numeric. | |
regex:<regexname> - validates only if the value matches the regex. Regexes are listed bewow. | |
notregex:<regexname> - validates only if the value does not match the regex. Regexes are listed below. | |
Regexes are (so far): | |
'name' - /[A-Z- ]+/i, can only contain A-Z, -, or space. Must have at least one. Case-insensitive. | |
'letters' - /[A-Z]+/i, can only contain A-Z. Must have at least one. Case-insensitive. | |
'email' - /^[\w\d%+-.]+(?<![._%+-])@(?:[A-Z\d-]+\.)+[A-Z]{2,4}$/i, matches a valid email address. Regex currently under improvement. | |
'phone-us-ext' - /\d{3}[-. ]\d{3}[-. ](?:\d{4}|\d{2}[-. ]\d{2})(?: ?(?:(?:x|ex|ext|extension) ?\d{1-5}))?/i, matches any valid US phone number, delimited by ., -, or a space, with optional 1-5 digit extension (specified by x, ex, ext, or extension, case-insensitive) | |
'phone-us' - /\d{3}[-. ]\d{3}[-. ](?:\d{4}|\d{2}[-. ]\d{2})/, matches any valid US phone number, delimited by ., -, or a space, without optional extension. | |
As a shorthand method of calling regexes, you can just use the regex name, IE '...|email|...' maps to '...|regex:email|...' | |
The same works with notregex, if you prepend an ! to the directive, IE '...|!letters|...' maps to '...|notregex[letters]|...' | |
WARNING: If you prepend any directive with !, it will automatically assume that it is a notregex directive. | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment