### Install Pristine.js via npm
Source: https://github.com/sha256/pristine/blob/master/README.md
Use npm to install the Pristine.js library as a project dependency.
```sh
$ npm install pristinejs --save
```
--------------------------------
### Basic Form Validation with Pristine.js
Source: https://github.com/sha256/pristine/blob/master/README.md
Instantiate Pristine with your form element and use the validate() method on form submission. This example prevents default submission and checks validity.
```javascript
window.onload = function () {
var form = document.getElementById("form1");
// create the pristine instance
var pristine = new Pristine(form);
form.addEventListener('submit', function (e) {
e.preventDefault();
// check if the form is valid
var valid = pristine.validate(); // returns true or false
});
};
```
--------------------------------
### Pristine Constructor
Source: https://github.com/sha256/pristine/blob/master/README.md
Initializes a new Pristine instance to manage form validation.
```APIDOC
## Pristine(form, config, live)
### Description
Initializes a new Pristine instance to manage form validation.
### Parameters
#### Path Parameters
- **form** (DOM Element) - Required - The form element to be validated.
- **config** (Object) - Optional - The configuration object for Pristine. Defaults to global configuration.
- **live** (Boolean) - Optional - Whether Pristine should validate as the user types. Defaults to true.
```
--------------------------------
### Default Pristine.js Configuration Object
Source: https://github.com/sha256/pristine/blob/master/README.md
This object shows the default configuration for Pristine.js, including classes for error/success states and where error messages are appended.
```javascript
let defaultConfig = {
// class of the parent element where the error/success class is added
classTo: 'form-group',
errorClass: 'has-danger',
successClass: 'has-success',
// class of the parent element where error text element is appended
errorTextParent: 'form-group',
// type of element to create for the error text
errorTextTag: 'div',
// class of the error text element
errorTextClass: 'text-help'
};
```
--------------------------------
### pristine.setGlobalConfig
Source: https://github.com/sha256/pristine/blob/master/README.md
Sets the global configuration object for all Pristine instances.
```APIDOC
## pristine.setGlobalConfig(config)
### Description
Sets the global configuration object for all Pristine instances.
### Parameters
#### Path Parameters
- **config** (Object) - Required - The configuration object to set globally.
```
--------------------------------
### Include Pristine.js in HTML
Source: https://github.com/sha256/pristine/blob/master/README.md
Include the Pristine.js script in your HTML file. This can be placed in the head or just before the closing body tag.
```html
```
--------------------------------
### pristine.validate
Source: https://github.com/sha256/pristine/blob/master/README.md
Validates the form or specific input fields.
```APIDOC
## pristine.validate(inputs, silent)
### Description
Validates the form or specific input fields.
### Parameters
#### Path Parameters
- **inputs** (DOM Element or Collection) - Optional - The input element(s) to validate. If not provided, the entire form is validated.
- **silent** (Boolean) - Optional - If true, error messages will not be displayed. Defaults to false.
```
--------------------------------
### Add a Global Custom Validator
Source: https://github.com/sha256/pristine/blob/master/README.md
Define a custom validator that can be applied to any field. Global validators must be added before initializing the Pristine instance.
```javascript
// A validator to check if the input value is within a specified range
// Global validators must be added before creating the pristine instance
Pristine.addValidator("my-range", function(value, param1, param2) {
// here `this` refers to the respective input element
return parseInt(param1) <= value && value <= parseInt(param2)
}, "The value (${0}) must be between ${1} and ${2}", 5, false);
```
--------------------------------
### pristine.destroy
Source: https://github.com/sha256/pristine/blob/master/README.md
Destroys the Pristine instance, removing event listeners and cleaning up.
```APIDOC
## pristine.destroy()
### Description
Destroys the Pristine instance, removing event listeners and cleaning up.
```
--------------------------------
### Pristine.setLocale
Source: https://github.com/sha256/pristine/blob/master/README.md
Sets the global locale for error messages.
```APIDOC
## Pristine.setLocale(locale)
### Description
Sets the global locale for error messages.
### Parameters
#### Path Parameters
- **locale** (String) - Required - The locale to set for error messages.
```
--------------------------------
### Pristine.addMessages
Source: https://github.com/sha256/pristine/blob/master/README.md
Adds custom error messages for a specific locale.
```APIDOC
## Pristine.addMessages(locale, messages)
### Description
Adds custom error messages for a specific locale.
### Parameters
#### Path Parameters
- **locale** (String) - Required - The locale for which to add messages.
- **messages** (Object) - Required - An object where keys are validator names and values are the corresponding error message strings.
```
--------------------------------
### pristine.getErrors
Source: https://github.com/sha256/pristine/blob/master/README.md
Retrieves the validation errors for the form or a specific input.
```APIDOC
## pristine.getErrors(input)
### Description
Retrieves the validation errors for the form or a specific input.
### Parameters
#### Path Parameters
- **input** (DOM Element) - Optional - The input element for which to retrieve errors. If not provided, all form errors are returned.
### Returns
- Object - An object containing all form errors, or an array of errors for a specific input. `validate()` must be called prior to using this method.
```
--------------------------------
### pristine.reset
Source: https://github.com/sha256/pristine/blob/master/README.md
Resets all validation errors in the form.
```APIDOC
## pristine.reset()
### Description
Resets all validation errors in the form.
```
--------------------------------
### pristine.addError
Source: https://github.com/sha256/pristine/blob/master/README.md
Manually adds a custom error message to a specific input element.
```APIDOC
## pristine.addError(input, error)
### Description
Manually adds a custom error message to a specific input element.
### Parameters
#### Path Parameters
- **input** (DOM Element) - Optional - The input element to which the error should be added.
- **error** (String) - Required - The custom error message to add.
```
--------------------------------
### Pristine.addValidator
Source: https://github.com/sha256/pristine/blob/master/README.md
Adds a global custom validator that can be applied using data attributes.
```APIDOC
## Pristine.addValidator(name, fn, message, priority, halt)
### Description
Adds a global custom validator that can be applied using data attributes.
### Parameters
#### Path Parameters
- **name** (String) - Required - The name of the validator. This name can be used in `data-pristine-` attributes.
- **fn** (Function) - Required - The validation function. It receives the input value and attribute values as arguments. `this` refers to the input element.
- **message** (String or Function) - Required - The error message to display if validation fails. Supports templating with `${0}`, `${1}`, etc. for attribute values.
- **priority** (Number) - Optional - The priority of the validator. Higher values are executed earlier. Defaults to 1.
- **halt** (Boolean) - Optional - If true, validation stops on this field after this validator runs. Defaults to false.
```
--------------------------------
### Assign Global Custom Validator in HTML
Source: https://github.com/sha256/pristine/blob/master/README.md
Use a data attribute to assign a globally defined custom validator to an HTML input field, specifying parameters as needed.
```html
```
--------------------------------
### pristine.addValidator
Source: https://github.com/sha256/pristine/blob/master/README.md
Adds a custom validator to a specific DOM element.
```APIDOC
## pristine.addValidator(elem, fn, message, priority, halt)
### Description
Adds a custom validator to a specific DOM element.
### Parameters
#### Path Parameters
- **elem** (DOM Element) - Required - The DOM element to which the validator is applied.
- **fn** (Function) - Required - The validation function. It receives the input value and attribute values as arguments. `this` refers to the input element.
- **message** (String or Function) - Required - The error message to display if validation fails. Supports templating with `${0}`, `${1}`, etc. for attribute values.
- **priority** (Number) - Optional - The priority of the validator. Higher values are executed earlier. Defaults to 1.
- **halt** (Boolean) - Optional - If true, validation stops on this field after this validator runs. Defaults to false.
```
--------------------------------
### Add a Custom Validator to a Specific Field
Source: https://github.com/sha256/pristine/blob/master/README.md
Register a custom validator function for a specific form element. The handler function receives the element's value and can return true or false.
```javascript
var pristine = new Pristine(document.getElementById("form1"));
var elem = document.getElementById("email");
// A validator to check if the first letter is capitalized
pristine.addValidator(elem, function(value) {
// here `this` refers to the respective input element
if (value.length && value[0] === value[0].toUpperCase()){
return true;
}
return false;
}, "The first character must be capitalized", 2, false);
```
--------------------------------
### Add Custom Error Message for Required Field
Source: https://github.com/sha256/pristine/blob/master/README.md
Provide a custom error message for a specific validator by adding a data attribute to the input element.
```html
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.