Valid8r Kitchen Sink Examples

Validate Strings

{"rule":"len", "min":5}
Validate the length of a value.
{"rule":"len", "max":20}
{"rule":"len", "min":5, "max":20}
{"rule":"isAlpha"}
Validate that a value contains all alphabetic characters (a-z, A-Z)
{"rule":"isNum"}
Validate that a value contains all numeric characters (0-9)
{"rule":"isNum", "nonNeg":true}
Validate that a value contains a non-negative numeric number (+0-9)
{"rule":"isAlnum"}
Validate that a value is all Alpha-numeric characters
{"rule":"formatted_as", "format":"DD/DD/DDDD"}
Validate that a value matches a format of letters, numbers and other symbols.
{"rule":"formatted_as", "format":"(DDD) DDD-DDDD"}
{"rule":"formatted_as", "format":"AADDAADDDDDD"}
{"rule":"regex", pattern:"[a-Z0-9.-]{2,7}", modifiers:"i"}
Validate that a value using a regular expression.

Validate Numbers

{"rule":"val","min":5}
Validate a numeric value.
{"rule":"val","max":10}
{"rule":"val","min":5,"max":10}
{"rule":"val", "outside":[5,10]}

Special Validators

{"rule":"email"}
Validate email addresses. (several options available on this.)
{"rule":"url"}
Validate a URL
{"rule":"url", "protocols":["http","https","ftp","git"]}
http://
{"rule":"url", "noProtocols":true}
{"rule":"ip"}
Validate an IP address - any version.
{"rule":"ip",v:4}
Validate an IP address, version 4 only.
{"rule":"ip",v:6}
Validate an IP address, version 6 only.

Validate Checkoxes & Radio Buttons

{"rule":"checks", "min":3}
Validate Checkboxes
{"rule":"checks", "max":3}
{"rule":"checks", "min":3,"max":6}
{"rule":"radios"}
Validate Radio Buttons

Conditional Validation

 
Validate only when other conditions are met.
{
"rule": "val", "is":7, 
"when": {
"selector": "#validate_answer:checked", // do we need this? 
"field": "validate_next_field",
"eq": "1"
}
}

Custom Validator Functions

{"rule":"custom", "func":"customValidator"}
Validates a value against a custom validation function of your own. The one used here requires you type the word custom into the text field.
{"rule":"custom", "func":"customAsynchronousValidator", "async":"true"}
Validates a value against a custom validation function of your own. The async property tells Valid8r that your function uses AJAX to perform validation, and to skip it when validating the form onSubmit.

JSON used for the Kitchen Sink

 { 
	"_globalConditions": {
		"validate_answer": {
			"selector": "#validate_answer:checked",
			"field": "validate_answer",
			"is" : "1"
		}
	},
	"_rules" : {
		"answer": {
			"selector": "#answer",
			"rules": [
				{"rule": "required", "when": "validate_answer", "errStr": "Please enter an answer."},
				{"rule": "val", "is": 7, "when": "validate_answer", "errStr" : "Wrong answer, please try again."}
			]
		},
		"custom": {
			"selector": "#custom",
			"rules": [
				{"rule":"custom", "func":"customValidator"}
			]
		},
		"custom_async": {
			"rules": [
				{"rule":"custom", "func":"customAsynchronousValidator"}
			]
		},
		"min_len": {
			"rules": [
				{"rule":"len", "min":5, "errStr": "Please enter at least 5 characters."}
			]
		},
		"max_len": {
			"rules": [
				{"rule":"len", "max":20, "errStr": "Please enter no more than 20 characters."}
			]
		},
		"min_max_len": {
			"rules": [
				{"rule":"len", "min":5,"max":20, "errStr": "Please enter between 5 and 20 characters."}
			]
		},
		"alpha":{
			"rules": [
				{"rule":"isAlpha", "errStr": "Please enter letters only."}
			]
		},
		"num":{
			"rules": [
				{"rule":"isNum", "errStr": "Please enter numbers only."}
			]
		},
		"num_pos":{
			"rules": [
				{"rule":"isNum", "nonNeg":true, "errStr": "Please enter numbers only."}
			]
		},
		"alnum":{
			"rules": [
				{"rule":"isAlnum", "errStr": "Please enter letters and numbers only."}
			]
		},
		"formatted_as":{
			"rules": [
				{"rule":"formattedAs", "format":"DD/DD/DDDD"}
			]
		},
		"formatted_as2":{
			"rules": [
				{"rule":"formattedAs", "format":"(DDD) DDD-?DDDD"}
			]
		},
		"formatted_as3":{
			"rules": [
				{"rule":"formattedAs", "format":"AADDAADDDDDD"}
			]
		},
		"regex":{
			"rules": [
				{"rule":"regex", "pattern":"[A-z0-9.-]{2,7}", "modifiers":"i"}
			]
		},
		"min_val":{
			"rules": [
				{"rule":"val","min":5, "errStr": "Must be a minimum of 5.", "nanErrStr": "Please enter a number."}
			]
		},
		"max_val":{
			"rules": [
				{"rule":"val","max":10, "errStr": "Must be a maximum of 10.", "nanErrStr": "Please enter a number."}
			]
		},
		"min_max_val":{
			"rules": [
				{"rule":"val","min":5, "max":10, "errStr": "Must be a minimum of 5 and maximum of 10.", "nanErrStr": "Please enter a number."}
			]
		},
		"val_outside":{
			"rules": [
				{"rule":"val","outside":[5,10]}
			]
		},
		"email":{
			"rules": [
				{"rule":"email"}
			]
		},
		"url":{
			"rules": [
				{"rule":"url"}
			]
		},
		"ip":{
			"rules": [
				{"rule":"ip"}
			]
		},
		"ipv4":{
			"rules": [
				{"rule":"ip","v":4}
			]
		},
		"ipv6":{
			"rules": [
				{"rule":"ip","v":6}
			]
		},
		"url_protocols":{
			"rules": [
				{"rule":"url", "protocols":["http","https","ftp","git"]}
			]
		},
		"url_withoutProtocols":{
			"rules": [
				{"rule":"url","noProtocols": true}
			]
		},
		"checkboxes_min_3" : {
			"selector": "input[name=\"checkboxes_min_3[]\"]",
			"type":"checks",
			"rules" : [
				{"rule":"checks","min":3}
			]
		},
		"checkboxes_max_3" : {
			"selector": "input[name=\"checkboxes_max_3[]\"]",
			"type":"checks",
			"rules" : [
				{"rule":"checks","max":3}
			]
		},
		"checkboxes_min_3_max4" : {
			"selector": "input[name=\"checkboxes_min_3_max4[]\"]",
			"type":"checks",
			"rules" : [
				{"rule":"checks","min":3,"max":4}
			]
		},
		"radios":{
			"selector": "input[name=\"radios\"]",
			"type":"radios",
			"rules": [
				{"rule":"radios"}
			]
		}
		
	}
}