https://github.com/lerhhl/js-textfield-validation
An npm Package to validate textfield value.
https://github.com/lerhhl/js-textfield-validation
javascript javascript-library textfield textfield-validation validation validation-library
Last synced: 11 months ago
JSON representation
An npm Package to validate textfield value.
- Host: GitHub
- URL: https://github.com/lerhhl/js-textfield-validation
- Owner: lerhhl
- License: mit
- Created: 2019-04-01T09:31:15.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:40:57.000Z (over 3 years ago)
- Last Synced: 2025-03-15T10:33:52.141Z (about 1 year ago)
- Topics: javascript, javascript-library, textfield, textfield-validation, validation, validation-library
- Language: HTML
- Homepage: https://www.npmjs.com/package/js-textfield-validation
- Size: 215 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Textfield Validation
An npm Package to validate textfield value.
## How to install
```bash
# with npm
npm install js-textfield-validation
```
## API
There are chainable and non-chainable methods.
### Available chainable validations
| Methods | Description |
| --- | --- |
| `alphanumericOnly()` | To accept alphanumeric only. |
| `dollarValue()` | To create a value with two decimal places. |
| `ipAddress()` | To accept number and dot only. |
| `noSpace()` | To remove all the spaces. |
| `numOnly()` | To remove all the non integer. |
| `removeNum()` | To remove all the number. |
| `removeLeadingZero()` | To remove all the leading zero. |
| `singleSpace()` | To accept single space between two characters only. |
| `truncate(length: integer)` | To truncate the value to a specifc length. |
| `wordOnly()` | To remove all non alphabet. |
### Available non-chainable validations
| Methods | Description | Output | Remark |
| --- | --- | --- | --- |
| `validateAlphanumericOnly(value: string)` | To check whether the value contains alphanumberic only. | `boolean` | nil |
| `validateEmail(email: string)` | To check whether value is a valid email format. | `boolean` | nil |
| `validateNRIC(nric: string)` | To check whether value is an valid NRIC in Singapore. | `boolean` | [Source](http://www.samliew.com/icval/) |
| `validateIPAddress(address: string)` | To check whether value is a valid IP address. | `boolean` | nil |
## HOW TO USE
### Include chainable methods
```JS
import Validation from "js-textfield-validation";
```
### Include non-chainable methods
```JS
import { validateEmail, validateIPAddress, validateNRIC } from "js-textfield-validation";
```
### An example with ReactJS, material-ui and chainable methods
```JS
import React, { Component } from "react";
import TextField from "@material-ui/core/TextField";
import Validation from "js-textfield-validation";
class App extends Component {
constructor() {
super();
this.state = {
name: "",
error: "",
};
};
handleChange = event => {
let validatedName = new Validations(event.target.value).removeNum().singleSpace();
if (validatedName.error !== "") {
this.setState({ name: validatedName.value, error: validatedName.error });
} else {
this.setState({ name: validatedName, error: "" });
}
};
render() {
return (
);
};
};
```
### An example with ReactJS, material-ui and chainable and non-chainable methods
```JS
import React, { Component } from "react";
import TextField from "@material-ui/core/TextField";
import Validation, { validateEmail } from "js-textfield-validation";
class App extends Component {
constructor() {
super();
this.state = {
email: "",
errorMessage: ""
};
};
handleChange = event => {
let newEmail = new Validation(event.target.value).noSpace().value;
const isValidEmail = validateEmail(newEmail);
if (isValidEmail) {
this.setState({ email: newEmail, errorMessage: "" })
} else {
this.setState({ email: newEmail, errorMessage: "Invalid email" })
}
};
render() {
return (
{ this.state.errorMessage }
);
};
};
```
## LICENSE
LICENSE.md