Available validation rules
Introduction
Adding Validation to a form
- The example form
- Including rsv.js
- Defining your validation rules
- Adding the onsubmit handler
- The finished product
References
As mentioned earlier, the validation rules are all of the following form:
[if:FIELD=VAL,]RULE,fieldname[,fieldname2[,fieldname3,date_flag]],error message"
if:FIELDNAME!=VALUE,
This allows us to only validate a field only if a fieldname has - or doesn't have - a particular value. This option allows for nesting; i.e. you can have multiple if clauses, separated by commas. They will be examined in the order in which they appear in the line.
RULE
Valid RULE strings are:
fieldname: MONTH field
fieldname2: DAY field
fieldname3: YEAR field
date_flag: "later_date" / "any_date"
// this requires the "field_name" form field be of the form:
// {any letter}{uc letter}{lc consonant}{lc consonant}
rules.push("custom_alpha,field_name,DLcc,Error message");
Here's all the different formatting options and what each character means:
L | An uppercase letter. | V | An uppercase vowel. |
---|---|---|---|
l | A lowercase letter. | v | A lowercase vowel. |
D | A letter (upper or lower) | F | A vowel (upper or lower) |
C | An uppercase Consonant | x | Any number, 0-9 |
c | A lowercase consonant | X | Any number, 1-9 |
E | A consonant (upper or lower) |
Any characters included in the string that aren't in the above list are simply required as they are. So if your rules was CVC-VCV, the user would have to enter consonant, vowel, consonant followed by a dash, followed by vowel, consonant, vowel.
This option is for programmers only. It lets you validate a field by a regular expression. This rule comes in two forms: one with a RegExp flag (like "g" for global, "i" for case-insensitive etc.) and one without. Unless you expressly need to supply a flag, just use the first form.
Form 1:
rules.push("reg_exp,field_name,REGEXP,Error message");
Form 2:
rules.push("reg_exp,field_name,REGEXP,Flag(s),Error message");
Note: be sure to double-escape regexp escape characters. e.g. to match whitespace, the \s needs to be \\s; commas also need to be double escaped: \\,
Notes: With the digits_only, valid_email and custom_alpha rules, if the empty string is passed in it won't generate an error, thus allowing validation of non-required fields. So, if you want a field to be a valid email address, for example, provide separate rules for both "required" and "valid_email" for that single form field.