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 IPv4 address.
{"rule":"ip", "v":4}
Validate an IPv6 address.

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":"customValidatorFunction"}
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":"instance_validator", "php_class":"CustomValidatorClass"}
Add the php_class property to your rule to tell Valid8r to instantiate an object of that class, and then call the method you've designated by the func property.
{"rule":"custom", "func":"static_validator", "php_static_class":"CustomValidatorClass"}
You can use the php_static_class method to tell Valid8r to look for func as a static method of that class.
{"rule":"custom", "func":"instance_validator", "php_class":"CustomNamespacedValidatorClass", "php_namespace":"CustomValidatorNamespace"}
Add a php_namespace property to your rule to tell Valid8r to look for php_class in that namespace.
{"rule":"custom", "func":"static_validator", "php_static_class":"CustomNamespacedValidatorClass", "php_namespace":"CustomValidatorNamespace"}
You can also namespace static class methods...

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":"customValidatorFunction"}
      ]
    },
    "custom_static": {
      "selector": "#custom_static",
      "rules": [
        {"rule":"custom", "func":"static_validator", "php_static_class":"CustomValidatorClass"}
      ]
    },
    "custom_instance_method": {
      "selector": "#custom_instance_method",
      "rules": [
        {"rule":"custom", "func":"instance_validator", "php_class":"CustomValidatorClass"}
      ]
    },
    "custom_instance_namespaced": {
      "selector": "#custom_instance_namespaced",
      "rules": [
        {"rule":"custom", "func":"instance_validator", "php_class":"CustomNamespacedValidatorClass", "php_namespace":"CustomValidatorNamespace"}
      ]
    },
    "custom_static_namespaced": {
      "selector": "#custom_static_namespaced",
      "rules": [
        {"rule":"custom", "func":"static_validator", "php_static_class":"CustomNamespacedValidatorClass", "php_namespace":"CustomValidatorNamespace"}
      ]
    },
    "min_len": {
      "selector": "#min_len",
      "rules": [
        {"rule":"len", "min":5, "errStr": "Please enter at least 5 characters."}
      ]
    },
    "max_len": {
      "selector": "#max_len",
      "rules": [
        {"rule":"len", "max":20, "errStr": "Please enter no more than 20 characters."}
      ]
    },
    "min_max_len": {
      "selector": "#min_max_len",
      "rules": [
        {"rule":"len", "min":5,"max":20, "errStr": "Please enter between 5 and 20 characters."}
      ]
    },
    "alpha":{
      "selector": "#alpha",
      "rules": [
        {"rule":"isAlpha", "errStr": "Please enter letters only."}
      ]
    },
    "num":{
      "selector": "#num",
      "rules": [
        {"rule":"isNum", "errStr": "Please enter numbers only."}
      ]
    },
    "num_pos":{
      "selector": "#num_pos",
      "rules": [
        {"rule":"isNum", "nonNeg":true, "errStr": "Please enter numbers only."}
      ]
    },
    "alnum":{
      "selector": "#alnum",
      "rules": [
        {"rule":"isAlnum", "errStr": "Please enter letters and numbers only."}
      ]
    },
    "formatted_as":{
      "selector": "#formatted_as",
      "rules": [
        {"rule":"formattedAs", "format":"DD/DD/DDDD"}
      ]
    },
    "formatted_as2":{
      "selector": "#formatted_as2",
      "rules": [
        {"rule":"formattedAs", "format":"(DDD) DDD-DDDD"}
      ]
    },
    "formatted_as3":{
      "selector": "#formatted_as3",
      "rules": [
        {"rule":"formattedAs", "format":"AADDAADDDDDD"}
      ]
    },
    "regex":{
      "selector": "#regex",
      "rules": [
        {"rule":"regex", "pattern":"[A-z0-9.-]{2,7}", "modifiers":"i"}
      ]
    },
    "min_val":{
      "selector": "#min_val",
      "rules": [
        {"rule":"val","min":5, "errStr": "Must be a minimum of 5.", "nanErrStr": "Please enter a number."}
      ]
    },
    "max_val":{
      "selector": "#max_val",
      "rules": [
        {"rule":"val","max":10, "errStr": "Must be a maximum of 10.", "nanErrStr": "Please enter a number."}
      ]
    },
    "min_max_val":{
      "selector": "#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":{
      "selector": "#val_outside",
      "rules": [
        {"rule":"val","outside":[5,10]}
      ]
    },
    "email":{
      "selector": "#email",
      "rules": [
        {"rule":"email"}
      ]
    },
    "url":{
      "selector": "#url",
      "rules": [
        {"rule":"url"}
      ]
    },
    "url_protocols":{
      "selector": "#url_protocols",
      "rules": [
        {"rule":"url", "protocols":["http","https","ftp","git"]}
      ]
    },
    "url_withoutProtocols":{
      "selector": "#url_withoutProtocols",
      "rules": [
        {"rule":"url","noProtocols": true}
      ]
    },
    "ip":{
      "rules": [
        {"rule":"ip"}
      ]
    },
    "ipv4":{
      "rules": [
        {"rule":"ip","v":4}
      ]
    },
    "ipv6":{
      "rules": [
        {"rule":"ip","v":6}
      ]
    },
    "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"}
      ]
    }

  }
}