# wire.validate

Validate data entered or selected by users using the `wire.validate` method.  The method will return `null` if the validation was successful.  It will return an error message if it was not.

## Parameters

| Name   | Description                           |
| ------ | ------------------------------------- |
| value  | The value to be validated.            |
| config | The configuration for the validation. |

## Config Properties

Use the following properties on the second parameter `config` to configure the validation.

| Name     | Description                                                                           |
| -------- | ------------------------------------------------------------------------------------- |
| type     | The type of validation to be performed.  See valid values [below](#validation-types). |
| error    | Error message to be returned if the validation fails.                                 |
| required | Determines whether the `value` passed into the `validate` method is required.         |

## Validation Types

The following details the valid values of the type configuration property along with the additional configuration for each.

### Number

Use the `number` type to be sure a value is a valid number.

```javascript
// returns null
const error = wire.validate(10, {    
    type: "number"
});
```

Use `gt` with  the `number` type to be sure a value is greater than a specific value.

```javascript
// returns value in error
const error = wire.validate(10, {    
    type: "number", 
    gt: 20
    error: "Please enter a number greater than 20"
});
```

### Value

Use the `value` type to verify that a value is within a list of `values`.

```javascript
// returns null
const error = wire.validate("Y", {    
    type: "value", 
    values: [ "Y", "N" ]
});
```

Use `case` with the `value` type to specify whether the check against the `values` list should be case sensitive.  The `case` condition defaults to `false`.

```javascript
// returns default error message for 'value' type
const error = wire.validate("y", {    
    type: "value", 
    case: true,
    values: [ "Y", "N" ]
});
```

### Date

Use the `date` type to verify that a value is a valid date. &#x20;

```javascript
// returns null
const error = wire.validate("01-15-1986", {    
    type: "date" 
});
```

### Phone Number

Use the `phone` type to verify a value is a valid phone number. &#x20;

```javascript
// returns null
const error = wire.validate("201-555-1212", {    
    type: "phone" 
});
```

### Email Address

Use the `email` type to verify that a value is a valid email address. &#x20;

```javascript
// returns null
const error = wire.validate("jkratz@gmail.com", {    
    type: "email" 
});
```

### Regular Expression

Use the `regex` type to verify that a value meets a regular expression condition.  Set the expression into the `exp` property. &#x20;

The following checks for a valid phone number.

```javascript
// returns null
const error = wire.validate("201-555-1212", {    
    type: "regex",
    exp: "^\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$" 
});
```

{% hint style="info" %}
Note, the site <https://www.regextester.com/> is a useful site for testing regular expressions for use with this type of validation.
{% endhint %}

### Custom

When all else fails, use the `custom` type to validate a value using a custom function.  Use the `validate` property to define the custom function.  This function takes the same parameters as the `validate` function itself and should return the same error if the validation is not successful.

The following uses the [moment.js](https://momentjs.com/) library to do a custom date validation.

```javascript
// returns null
const error = wire.validate("011586", {    
    type: "custom" 
    validate: (value, config) => {
        if(!moment(value, 'MMDDYY', true).isValid())
            return "Must be in format 'mmddyy' (i.e. 081587)";
    }
});
```

#### Value

When using the `custom` validation type, the value passed in can also be an object. &#x20;

```javascript
const data = {date: "011586", checkMe: false };

const error = wire.validate(data, {    
    type: "custom" 
    validate: (value, config) => {
        if(value.checkMe && !moment(value.date, 'MMDDYY', true).isValid())
            return "Must be in format 'mmddyy' (i.e. 081587)";
    }
});
```
