# laravel-validator-for-js

> A constructor function for data validation on both the server and client, inspired by the Laravel PHP framework. The API is very similar to that of the Laravel 3 Validator class.

Latest version **0.5.1** (published 2013-05-23) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install laravel-validator-for-js
pnpm add laravel-validator-for-js
yarn add laravel-validator-for-js
bun add laravel-validator-for-js
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2013-05-23 |
| First published | 2013-02-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.8.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1824 |
| Author | David |
| Maintainers | skaterdav85 |
| Keywords | validatorjs, data validation, validator, validate, validation, data, laravel, laravel 3 |

## Links

- npm: https://www.npmjs.com/package/laravel-validator-for-js
- Repository: https://github.com/skaterdav85/validatorjs
- npm.io page: https://npm.io/package/laravel-validator-for-js

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.5.1 (latest) — 2013-05-23
- 0.5.0 — 2013-05-23
- 0.4.0 — 2013-03-17
- 0.3.3 — 2013-03-13
- 0.3.2 — 2013-03-11
- 0.3.1 — 2013-03-01
- 0.3.0 — 2013-02-28
- 0.2.1 — 2013-02-27
- 0.2.0 — 2013-02-06

## README

# validatorjs

[![Build Status](https://travis-ci.org/skaterdav85/validatorjs.png?branch=master)](https://travis-ci.org/skaterdav85/validatorjs)

## Setup

#### Browser:
1. Include __validator.min.js__ script onto your page from the distribution folder _dist_.
2. Invoke the Validator constructor function. See below for details on Validator parameters and validation rules.

#### Node.js:
Install the Validator package from the NPM registry [https://npmjs.org/package/laravel-validator-for-js](https://npmjs.org/package/laravel-validator-for-js)

```
	npm install laravel-validator-for-js
```

```js
	var Validator = require('laravel-validator-for-js');
```

__Since version 0.4, the Validator constructor is immediately returned from the require call. I.e. You no longer have to do require('laravel-validator-for-js').Validator__

## Usage and Examples

The __1st argument__ to the constructor is an object that contains the data you want to validate. 

The __2nd argument__ is an object that contains the validation rules. 

#### Example 1:
```js
	var data = {
		email: 'johndoe@gmail.com'
	};
	
	var rules = {
		email: 'email'
	};

	var validation = new Validator(data, rules);
	
	validation.passes() // true
	validation.fails() // false
	
```

To apply validation rules to the _input_ object, use the same object key names for the _rules_ object.

#### Example 2:
```js
	var rules = {
		name: 'required|size:3',
		email: 'required|email'
	};

	var data = {
		name: '',
		email: ''
	};

	var validation = new Validator(data, rules);

	validation.fails(); // true

```

## Validation Rules

* required - Checks if the length of the String representation of the value is > 0

```
	username: 'required'
```

* email - Checks for an @ symbol followed by a period


```
	address: 'email'
```

* size - Validate that an attribute is a given length, or, if an attribute is numeric, is a given value


```
	duration: 'size:2'
```

* min - Validate that an attribute is at least a given size.

```
	payment: 'min:10'
```

* max - Validate that an attribute is no greater than a given size

```
	cost: 'max:100'
```

__Note: All minimum and maximum checks are inclusive.__

* numeric - Validate that an attribute is numeric. The string representation of a number will pass.

```
	age: 'numeric'
```

* url - Validate that an attribute is a URL

```
	link: 'url'
```

## Error Messages
This contructor will automatically generate error messages for validation rules that failed. You can use the __first__ method to fetch the first error message of a failing attribute. You can access all of the errors through the __errors__ property on the Validator instance. 

There is also an __errorCount__ property on the validation instance to specify the number of validation errors.

## Public Instance Methods

* passes() - returns boolean
* fails() - returns boolean
* first(attribute_name) - returns first error message for _string_ attribute_name, or _null_ if no error message exists

## Static Methods

* register(custom_rule_name, callbackFn, errorMessage) - register a custom validation rule. 

If __callbackFn__ returns a truthy value, the validation will pass for this rule. Otherwise, this validation rule will fail. __errorMessage__ is an optional string where you can specify a custom error message. _:attribute_ inside errorMessage will be replaced with the attribute name.

```js
	Validator.register('telephone', function(val) {
		return val.match(/^\d{3}-\d{3}-\d{4}$/);
	}, 'The :attribute phone number is not in the format XXX-XXX-XXXX.');
```

## Testing

Install node module dependencies

```
	npm install
```

See __SpecRunner.html__ for Jasmine tests in the browser. 

You can also run the jasmine tests via Node.js once you've installed the NPM package jasmine-node.

```
	jasmine-node spec/ --verbose --color

	//OR

	npm test (which calls the above command)
```

Once the above test passes, run the following command which will in turn run JSHint and minify the source
```
	grunt
```

---
_Source: https://npm.io/package/laravel-validator-for-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
