js-textfield-validation
Advanced tools
Comparing version 1.0.3 to 1.0.4
39
index.js
@@ -60,8 +60,41 @@ export default class Validation { | ||
/** Check whether value is an valid email format */ | ||
Validation.prototype.validateEmail = function() { | ||
/** Check whether value is a valid email format */ | ||
export const validateEmail = email => { | ||
let re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
let valid = false; | ||
if (re.test(this.value)) valid = true; | ||
if (re.test(email)) valid = true; | ||
return valid; | ||
} | ||
/** Check whether the value is a valid NRIC in Singapore */ | ||
export const validateNRIC = nric => { | ||
if (nric.length !== 9) return false; | ||
nric = nric.toUpperCase(); | ||
var i, icArray = []; | ||
for (i = 0; i < 9; i++) { icArray[i] = nric.charAt(i); } | ||
icArray[1] = parseInt(icArray[1], 10) * 2; | ||
icArray[2] = parseInt(icArray[2], 10) * 7; | ||
icArray[3] = parseInt(icArray[3], 10) * 6; | ||
icArray[4] = parseInt(icArray[4], 10) * 5; | ||
icArray[5] = parseInt(icArray[5], 10) * 4; | ||
icArray[6] = parseInt(icArray[6], 10) * 3; | ||
icArray[7] = parseInt(icArray[7], 10) * 2; | ||
var weight = 0; | ||
for (i = 1; i < 8; i++) { weight += icArray[i]; } | ||
var offset = (icArray[0] === "T" || icArray[0] === "G") ? 4 : 0; | ||
var temp = (offset + weight) % 11; | ||
var st = ["J","Z","I","H","G","F","E","D","C","B","A"]; | ||
var fg = ["X","W","U","T","R","Q","P","N","M","L","K"]; | ||
var theAlpha; | ||
if (icArray[0] === "S" || icArray[0] === "T") { theAlpha = st[temp]; } | ||
else if (icArray[0] === "F" || icArray[0] === "G") { theAlpha = fg[temp]; } | ||
return (icArray[8] === theAlpha); | ||
} |
{ | ||
"name": "js-textfield-validation", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "An npm Package to validate textfield value.", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "(https://github.com/lerhhl/textfield-validation)" | ||
"url": "https://github.com/lerhhl/js-textfield-validation" | ||
}, | ||
@@ -20,3 +20,11 @@ "dependencies": { | ||
"validation", | ||
"textfield" | ||
"textfield", | ||
"javascript", | ||
"validate", | ||
"NRIC", | ||
"Singapore", | ||
"email", | ||
"email-validation", | ||
"price", | ||
"price-validation" | ||
], | ||
@@ -23,0 +31,0 @@ "author": "Lerh Hwee Lern", |
@@ -6,2 +6,3 @@ # Textfield Validation | ||
## How to install | ||
```bash | ||
@@ -14,21 +15,27 @@ # with npm | ||
Some validations are chainable. | ||
There are chainable and non-chainable methods. | ||
### Available validations | ||
### Available chainable validations | ||
| Validation | Description | Response | Chainable | | ||
| Validation | Description | Response | | ||
| --- | --- | --- | | ||
|`alphanumericOnly` | To accept alphanumeric only. | `string` | | ||
|`dollarValue` | To create a value with two decimal places. | `string` | | ||
|`noSpace` | To remove all the spaces. | `string` | | ||
|`removeNum` | To remove all the number. | `string` | | ||
|`numOnly` | To remove all the non integer. | `string` | | ||
|`removeLeadingZero` | To remove all the leading zero. | `string` | | ||
|`singleSpace` | To accept single space between two characters only. | `string` | | ||
|`wordOnly` | To remove all non alphabet. | `string` | | ||
### Available non-chainable validations | ||
| Validation | Description | Response | Remark | | ||
| --- | --- | --- | --- | | ||
|`alphanumericOnly` | To accept alphanumeric only. | `string` | Yes | | ||
|`dollarValue` | To create a value with two decimal places. | `string` | Yes | | ||
|`noSpace` | To remove all the spaces. | `string` | Yes | | ||
|`removeNum` | To remove all the number. | `string` | Yes | | ||
|`numOnly` | To remove all the non integer. | `string` | Yes | | ||
|`removeLeadingZero` | To remove all the leading zero. | `string` | Yes | | ||
|`singleSpace` | To accept single space between two characters only. | `string` | Yes | | ||
|`validateEmail` | To check whether value is an valid email format. | `boolean` | No | | ||
|`wordOnly` | To remove all non alphabet. | `string` | Yes | | ||
|`validateEmail` | To check whether value is an valid email format. | `boolean` | | | ||
|`validateNRIC` | To check whether value is an valid NRIC in Singapore. | `boolean` | Based on http://www.samliew.com/icval/ | | ||
## HOW TO USE | ||
Simple example with ReactJS, material-ui and chainable methods: | ||
### An example with ReactJS, material-ui and chainable methods | ||
@@ -70,4 +77,48 @@ ```JS | ||
### An example with non-chainable method | ||
```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 ( | ||
<div> | ||
<TextField | ||
id="email" | ||
label"Email" | ||
variant="outlined" | ||
placeholder="Enter your email here." | ||
value={ this.state.email } | ||
onChange={ this.handleChange } | ||
/> | ||
<div>{ this.state.errorMessage }</div> | ||
</div> | ||
); | ||
}; | ||
}; | ||
``` | ||
## LICENSE | ||
LICENSE.md |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7967
92
121