js-textfield-validation
Advanced tools
Comparing version 1.0.7 to 1.0.8
89
index.js
export default class Validation { | ||
constructor(value) { | ||
this.value = value; | ||
this.error = ""; | ||
}; | ||
@@ -9,3 +10,8 @@ }; | ||
Validation.prototype.noSpace = function() { | ||
this.value = this.value.replace(/\s/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/\s/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -16,3 +22,8 @@ }; | ||
Validation.prototype.removeNum = function() { | ||
this.value = this.value.replace(/\d/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/\d/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -23,3 +34,8 @@ }; | ||
Validation.prototype.wordOnly = function() { | ||
this.value = this.value.replace(/[^a-zA-Z]/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/[^a-zA-Z]/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -30,3 +46,8 @@ }; | ||
Validation.prototype.singleSpace = function() { | ||
this.value = this.value.replace(/\s{2}/, " "); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/\s{2}/, " "); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -37,3 +58,8 @@ }; | ||
Validation.prototype.numOnly = function() { | ||
this.value = this.value.replace(/\D/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/\D/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -44,3 +70,8 @@ }; | ||
Validation.prototype.removeLeadingZero = function() { | ||
this.value = this.value.replace(/^0/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/^0/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -51,8 +82,13 @@ }; | ||
Validation.prototype.dollarValue = function() { | ||
this.value = this.value.replace(/(\D)|(^0)/, ""); | ||
if (this.value.length === 0) { | ||
this.value = '0'; | ||
} else if (this.value.length > 2) { | ||
this.value = this.value.slice(0, -2) + '.' + this.value.slice(-2); | ||
}; | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/(\D)|(^0)/, ""); | ||
this.error = ""; | ||
if (this.value.length === 0) { | ||
this.value = '0'; | ||
} else if (this.value.length > 2) { | ||
this.value = this.value.slice(0, -2) + '.' + this.value.slice(-2); | ||
}; | ||
} | ||
return this; | ||
@@ -63,3 +99,8 @@ }; | ||
Validation.prototype.alphanumericOnly = function() { | ||
this.value = this.value.replace(/[^a-zA-Z0-9]/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/[^a-zA-Z0-9]/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
@@ -70,6 +111,26 @@ }; | ||
Validation.prototype.ipAddress = function() { | ||
this.value = this.value.replace(/([^0-9.])|(^[.]+)/, ""); | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else { | ||
this.value = this.value.replace(/([^0-9.])|(^[.]+)/, ""); | ||
this.error = ""; | ||
} | ||
return this; | ||
} | ||
/** Truncate the value to a specific length */ | ||
Validation.prototype.truncate = function(limit) { | ||
if (typeof this.value !== "string") { | ||
this.error = "Only String input is allowed."; | ||
} else if (typeof limit !== "number") { | ||
this.error = "Only Number input for truncate is allowed."; | ||
} else if (this.value.length <= limit) { | ||
this.error = ""; | ||
} else { | ||
this.value = this.value.slice(0, limit); | ||
this.error = ""; | ||
} | ||
return this; | ||
} | ||
/** Check whether value is a valid email format */ | ||
@@ -76,0 +137,0 @@ export const validateEmail = email => { |
{ | ||
"name": "js-textfield-validation", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "An npm Package to validate textfield value.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,24 +18,37 @@ # Textfield Validation | ||
| Validation | Description | Response | | ||
| --- | --- | --- | | ||
|`alphanumericOnly` | To accept alphanumeric only. | `string` | | ||
|`dollarValue` | To create a value with two decimal places. | `string` | | ||
|`ipAddress` | To accept number and dot only. | `string` | | ||
|`noSpace` | To remove all the spaces. | `string` | | ||
|`numOnly` | To remove all the non integer. | `string` | | ||
|`removeNum` | To remove all the number. | `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` | | ||
| Validation | Description | Input | Output | | ||
| --- | --- | --- | --- | | ||
|`alphanumericOnly` | To accept alphanumeric only. | `nil` | `string` | | ||
|`dollarValue` | To create a value with two decimal places. | `nil` | `string` | | ||
|`ipAddress` | To accept number and dot only. | `nil` | `string` | | ||
|`noSpace` | To remove all the spaces. | `nil` | `string` | | ||
|`numOnly` | To remove all the non integer. | `nil` | `string` | | ||
|`removeNum` | To remove all the number. | `nil` | `string` | | ||
|`removeLeadingZero` | To remove all the leading zero. | `nil` | `string` | | ||
|`singleSpace` | To accept single space between two characters only. | `nil` | `string` | | ||
|`truncate` | To truncate the value to a specifc length. | `integer` | `string` | | ||
|`wordOnly` | To remove all non alphabet. | `nil` | `string` | | ||
### Available non-chainable validations | ||
| Validation | Description | Response | Remark | | ||
| --- | --- | --- | --- | | ||
|`validateEmail` | To check whether value is a valid email format. | `boolean` | | | ||
|`validateIPAddress` | To check whether value is a valid IP address. | `boolean` | | | ||
|`validateNRIC` | To check whether value is an valid NRIC in Singapore. | `boolean` | Based on http://www.samliew.com/icval/ | | ||
| Validation | Description | Input | Output | Remark | | ||
| --- | --- | --- | --- | --- | | ||
|`validateEmail` | To check whether value is a valid email format. | `nil` | `boolean` | | | ||
|`validateIPAddress` | To check whether value is a valid IP address. | `nil` | `boolean` | | | ||
|`validateNRIC` | To check whether value is an valid NRIC in Singapore. | `nil` | `boolean` | Based on http://www.samliew.com/icval/ | | ||
## 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 | ||
@@ -53,2 +66,3 @@ | ||
name: "", | ||
error: "", | ||
}; | ||
@@ -58,4 +72,8 @@ }; | ||
handleChange = event => { | ||
let validatedName = new Validations(event.target.value).removeNum().singleSpace().value; | ||
this.setState({ name: validatedName }); | ||
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: "" }); | ||
} | ||
}; | ||
@@ -73,2 +91,3 @@ | ||
onChange={ this.handleChange } | ||
helperText={ this.state.error } | ||
/> | ||
@@ -81,3 +100,3 @@ </div> | ||
### An example with non-chainable method | ||
### An example with ReactJS, material-ui and chainable and non-chainable methods | ||
@@ -84,0 +103,0 @@ ```JS |
@@ -102,2 +102,11 @@ var Validation = require("../../src/chainable"); | ||
}) | ||
describe("truncate", function() { | ||
it("truncate the value length", function() { | ||
expect(Validation.truncate("aaa", 2)).toEqual("aa"); | ||
expect(Validation.truncate("aa", 2)).toEqual("aa"); | ||
expect(Validation.truncate("a", 2)).toEqual("a"); | ||
expect(Validation.truncate("", 2)).toEqual(""); | ||
}) | ||
}) | ||
}) |
@@ -40,2 +40,10 @@ exports.noSpace = function(value) { | ||
return value.replace(/([^0-9.])|(^[.]+)/, ""); | ||
} | ||
exports.truncate = function(value, limit) { | ||
if (value.length <= limit) { | ||
return value | ||
} else { | ||
return value.slice(0, limit); | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19002
378
142
0