Choose your Language

JavaScript

Validation for JavaScript

GitHub Reop
Kitchen Sink Demo | Mocha Tests

Example:

var validator = new Valid8r({
  bindToBlur: true, // default is true now
  form: "#myForm",
  rules: [...],
  callback: function(field, err) { ... }
};
// callback will be called for every field.
// err will contain an error string or 
// null if no errors were present.
PHP

Validation for PHP

GitHub Reop
Kitchen Sink Demo

Example:

$validator = new Valid8r(Array(
  'rules_file' => '/path/to/your/rules.json',
  'data' => $_POST,
));
$errors = $validator->validateAll();
/* errors is now an associative array, 
where the keys are fields with errors, 
and the values are the errors themselves.  
Fields with no errors are not in the array. */
Node

Validation for Node

GitHub Reop
Kitchen Sink Demo available in Repo

Example:

v = new Valid8r({
  rules: require('./rules.json'),
  data: req.body
})
errors = v.validateAll()
// { field: error, field2: error }
// if no errors were found for a particular
// field, then it will not be present
// in the object.

Validate data with any of the above modules using same JSON configuration:

Example Rules JSON Used by all 3 languages

/* You add properties to a configuration object for each field
* you want to valiate, each being an object itself with properties.
* The selector property is used for JavaScript only. (optional)
* The rules property is an array of rules to validate the field with.
*/
{
  "my_field": {
    "selector": "#my_field", // not required when id=field
    "rules": [
      { "rule": "required" },
      { "rule": "len", "min": 5 }
    ]
  }
}
 
/* Validators can also be run conditionally: */
{
  "my_field": {
    "conditions": {
      "condition_key" : {
        "field" : "some_field",
        "is": 1
      }
    },
    "rules": [
      { "rule": "required", "when":"condition_key" }
    ]
  }
}
  
/* And validation conditions can be set globally, 
 * and then applied to multiple rules:
 * (notice the rules are now moved to _rules)
 */
{
  "_globalConditions": {
    "condition_key": {
      "field" : "some_field",
      "is" : 1
    }
  },
  "_rules": {
    "my_field": {
      "rules": [
        { "rule": "required", "when":"condition_key" }
      ]
    }
    "my_field2": {
      "rules": [
        { "rule": "required", "when":"condition_key" }
      ]
    }
  }
}
  

A More Practical Example w/ JSON breaking comments!

{
  "name": {
    "rules": [{ "rule": "required" }]
  },
  "email": {
    "selector": "input[name=\"email\"]", // example when ID != field
    "rules": [
      { "rule": "required" },
      { "rule": "email" }
    ]
  },
  "phone": {
    "conditions": {
      "contactByPhone": {       // the condition key...
        "field": "primary_contact",
        "is": "phone"
      }
    },
    "rules": [
      { 
        "rule": "required", 
        "when": "contactByPhone" // condition key from above... 
      },
      {
        "rule": "formatted_as", // we won't includ the condition here
        "(DDD) DDD-DDDD"        // as we still want it formatted
      }                         // properly if given optionally...
    ]
  },
  "custom": {
    "rules": [
      { "rule": "custom", "func": "myCustomValidator" }
    ]
  },
}
Visit the Valid8r Repo for more info on the Configuration JSON and validators available.