@dravishek/number-validate 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Barefacedbear
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,63 +1,69 @@
|
|
|
1
|
-
# NumberValidate
|
|
2
1
|
|
|
3
|
-
|
|
2
|
+
# @dravishek/number-validate
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
Angular 16+ directive to validate number length before and after decimal point.
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
## Installation
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
ng generate component component-name
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
|
|
8
|
+
1. Install the NPM package
|
|
14
9
|
|
|
15
10
|
```bash
|
|
16
|
-
|
|
11
|
+
npm install @dravishek/number-validate
|
|
17
12
|
```
|
|
18
13
|
|
|
19
|
-
|
|
14
|
+
2. In your application, import the Directive. This directive is standalone so you need to declare it in the `import[]`.
|
|
20
15
|
|
|
21
|
-
|
|
16
|
+
Example:
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
ng build number-validate
|
|
25
|
-
```
|
|
18
|
+
- For standalone components:
|
|
26
19
|
|
|
27
|
-
|
|
20
|
+
```typescript
|
|
21
|
+
import { AdrNumberValidateDirective } from '@dravishek/number-validate';
|
|
22
|
+
import { Component } from '@angular/core';
|
|
28
23
|
|
|
29
|
-
|
|
24
|
+
@Component({
|
|
25
|
+
selector: 'app-root',
|
|
26
|
+
standalone: true,
|
|
27
|
+
imports: [AdrNumberValidateDirective]
|
|
28
|
+
})
|
|
29
|
+
export class AppComponent {
|
|
30
|
+
title = 'adr-number-validate';
|
|
31
|
+
}
|
|
32
|
+
```
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
- For non-standalone components:
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
cd dist/number-validate
|
|
36
|
-
```
|
|
36
|
+
```typescript
|
|
37
|
+
import { AdrNumberValidateDirective } from '@dravishek/number-validate';
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
@NgModule({
|
|
40
|
+
declarations: [],
|
|
41
|
+
imports: [AdrNumberValidateDirective]
|
|
42
|
+
})
|
|
43
|
+
export class AppModule { }
|
|
44
|
+
```
|
|
42
45
|
|
|
43
|
-
##
|
|
46
|
+
## Usage
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
- For decimal numbers
|
|
46
49
|
|
|
47
|
-
```
|
|
48
|
-
|
|
50
|
+
```html
|
|
51
|
+
<input type="text" adrNumberValidate="2.5" />
|
|
49
52
|
```
|
|
50
53
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
For end-to-end (e2e) testing, run:
|
|
54
|
+
- For non decimal numbers
|
|
54
55
|
|
|
55
|
-
```
|
|
56
|
-
|
|
56
|
+
```html
|
|
57
|
+
<input type="text" adrNumberValidate="2" />
|
|
57
58
|
```
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
## Social
|
|
61
|
+
|
|
62
|
+
[](https://github.com/barefacedbear)
|
|
63
|
+
[](mailto:barefaced.bear2018@gmail.com)
|
|
64
|
+
[](https://www.linkedin.com/in/avishekdr-2611)
|
|
65
|
+
[](https://stackoverflow.com/users/11954878/avishekdr)
|
|
60
66
|
|
|
61
|
-
##
|
|
67
|
+
## License
|
|
62
68
|
|
|
63
|
-
|
|
69
|
+
[](https://choosealicense.com/licenses/mit/)
|
|
@@ -15,8 +15,16 @@ class AdrNumberValidateDirective {
|
|
|
15
15
|
* Possible type of patterns allowed: X, X.X
|
|
16
16
|
*/
|
|
17
17
|
adrNumberValidate = input('');
|
|
18
|
+
previousValue = '';
|
|
18
19
|
onKeyDown = (event) => this.execute(this.el.nativeElement.value);
|
|
19
20
|
onPaste = (event) => this.execute(this.el.nativeElement.value);
|
|
21
|
+
ngDoCheck() {
|
|
22
|
+
const currentValue = this.el.nativeElement.value;
|
|
23
|
+
if (currentValue !== this.previousValue) {
|
|
24
|
+
this.execute(this.previousValue);
|
|
25
|
+
this.previousValue = currentValue;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
20
28
|
checkValue(value) {
|
|
21
29
|
let [length, scale] = this.adrNumberValidate().split('.'), regExpString = `^(\\+|\\-)?([\\d]{0,${+length}})`, checkPattern = null;
|
|
22
30
|
if (+scale > 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dravishek-number-validate.mjs","sources":["../../../../projects/dravishek/number-validate/src/lib/adr-number-validate.directive.ts","../../../../projects/dravishek/number-validate/src/public-api.ts","../../../../projects/dravishek/number-validate/src/dravishek-number-validate.ts"],"sourcesContent":["import { Directive, ElementRef, HostListener, input, Optional } from '@angular/core';\r\nimport { NgControl } from '@angular/forms';\r\n\r\n@Directive({\r\n selector: '[adrNumberValidate]',\r\n standalone: true\r\n})\r\nexport class AdrNumberValidateDirective {\r\n\r\n constructor(private el: ElementRef, @Optional() private control: NgControl) { }\r\n\r\n /**\r\n * @description Value before the decimal point specifies the number of digits before decimal and value after the decimal specifies the number of digits after decimal.\r\n * Example: 7.3 (Before decimal 7 digits & 3 digits after decimal)\r\n * Possible type of patterns allowed: X, X.X\r\n */\r\n adrNumberValidate = input<string>('');\r\n\r\n @HostListener(\"keydown\", [\"$event\"])\r\n onKeyDown = (event: KeyboardEvent) => this.execute(this.el.nativeElement.value);\r\n\r\n @HostListener(\"paste\", [\"$event\"])\r\n onPaste = (event: ClipboardEvent) => this.execute(this.el.nativeElement.value);\r\n\r\n private checkValue(value: string): RegExpMatchArray | null {\r\n let [length, scale] = this.adrNumberValidate().split('.'), regExpString = `^(\\\\+|\\\\-)?([\\\\d]{0,${+length}})`,\r\n checkPattern: RegExpMatchArray | null = null;\r\n if (+scale > 0) { regExpString += `((\\\\.{1})([\\\\d]{1,${+scale}})?)` }\r\n checkPattern = String(value).match(new RegExp(`${regExpString}?$`));\r\n return checkPattern;\r\n }\r\n\r\n private execute(oldValue: string) {\r\n setTimeout(() => {\r\n let currentValue: string = this.el.nativeElement.value;\r\n if (currentValue && !this.checkValue(currentValue)) {\r\n this.control?.control && this.control.control.patchValue(+oldValue);\r\n this.el.nativeElement.value = oldValue;\r\n }\r\n });\r\n }\r\n}\r\n","/*\r\n * Public API Surface of number-validate\r\n */\r\n\r\nexport * from './lib/adr-number-validate.directive';\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAOa,0BAA0B,CAAA;AAEjB,IAAA,EAAA;AAAoC,IAAA,OAAA;IAAxD,WAAoB,CAAA,EAAc,EAAsB,OAAkB,EAAA;QAAtD,IAAE,CAAA,EAAA,GAAF,EAAE;QAAkC,IAAO,CAAA,OAAA,GAAP,OAAO;;AAE/D;;;;AAIE;AACF,IAAA,iBAAiB,GAAG,KAAK,CAAS,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"dravishek-number-validate.mjs","sources":["../../../../projects/dravishek/number-validate/src/lib/adr-number-validate.directive.ts","../../../../projects/dravishek/number-validate/src/public-api.ts","../../../../projects/dravishek/number-validate/src/dravishek-number-validate.ts"],"sourcesContent":["import { Directive, DoCheck, ElementRef, HostListener, input, Optional } from '@angular/core';\r\nimport { NgControl } from '@angular/forms';\r\n\r\n@Directive({\r\n selector: '[adrNumberValidate]',\r\n standalone: true\r\n})\r\nexport class AdrNumberValidateDirective implements DoCheck {\r\n\r\n constructor(private el: ElementRef, @Optional() private control: NgControl) { }\r\n\r\n /**\r\n * @description Value before the decimal point specifies the number of digits before decimal and value after the decimal specifies the number of digits after decimal.\r\n * Example: 7.3 (Before decimal 7 digits & 3 digits after decimal)\r\n * Possible type of patterns allowed: X, X.X\r\n */\r\n adrNumberValidate = input<string>('');\r\n private previousValue: string = '';\r\n\r\n @HostListener(\"keydown\", [\"$event\"])\r\n onKeyDown = (event: KeyboardEvent) => this.execute(this.el.nativeElement.value);\r\n\r\n @HostListener(\"paste\", [\"$event\"])\r\n onPaste = (event: ClipboardEvent) => this.execute(this.el.nativeElement.value);\r\n\r\n ngDoCheck(): void {\r\n const currentValue = this.el.nativeElement.value;\r\n if (currentValue !== this.previousValue) {\r\n this.execute(this.previousValue);\r\n this.previousValue = currentValue;\r\n }\r\n }\r\n \r\n private checkValue(value: string): RegExpMatchArray | null {\r\n let [length, scale] = this.adrNumberValidate().split('.'), regExpString = `^(\\\\+|\\\\-)?([\\\\d]{0,${+length}})`,\r\n checkPattern: RegExpMatchArray | null = null;\r\n if (+scale > 0) { regExpString += `((\\\\.{1})([\\\\d]{1,${+scale}})?)` }\r\n checkPattern = String(value).match(new RegExp(`${regExpString}?$`));\r\n return checkPattern;\r\n }\r\n\r\n private execute(oldValue: string) {\r\n setTimeout(() => {\r\n let currentValue: string = this.el.nativeElement.value;\r\n if (currentValue && !this.checkValue(currentValue)) {\r\n this.control?.control && this.control.control.patchValue(+oldValue);\r\n this.el.nativeElement.value = oldValue;\r\n }\r\n });\r\n }\r\n}\r\n","/*\r\n * Public API Surface of number-validate\r\n */\r\n\r\nexport * from './lib/adr-number-validate.directive';\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAOa,0BAA0B,CAAA;AAEjB,IAAA,EAAA;AAAoC,IAAA,OAAA;IAAxD,WAAoB,CAAA,EAAc,EAAsB,OAAkB,EAAA;QAAtD,IAAE,CAAA,EAAA,GAAF,EAAE;QAAkC,IAAO,CAAA,OAAA,GAAP,OAAO;;AAE/D;;;;AAIE;AACF,IAAA,iBAAiB,GAAG,KAAK,CAAS,EAAE,CAAC;IAC7B,aAAa,GAAW,EAAE;AAGlC,IAAA,SAAS,GAAG,CAAC,KAAoB,KAAK,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC;AAG/E,IAAA,OAAO,GAAG,CAAC,KAAqB,KAAK,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC;IAE9E,SAAS,GAAA;QACP,MAAM,YAAY,GAAG,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,KAAK;AAChD,QAAA,IAAI,YAAY,KAAK,IAAI,CAAC,aAAa,EAAE;AACvC,YAAA,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC;AAChC,YAAA,IAAI,CAAC,aAAa,GAAG,YAAY;;;AAI7B,IAAA,UAAU,CAAC,KAAa,EAAA;QAC9B,IAA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BAAA,EAAA,QAAA,EAAA,IAAA,EAAA,UAAA,EAAA,KAAA,EAAA,iBAAA,EAAA,IAAA,EAAA,EAAA,EAAA,IAAA,EAAA,EAAA,SAAA,EAAA,EAAA,SAAA,EAAA,mBAAA,EAAA,OAAA,EAAA,iBAAA,EAAA,EAAA,EAAA,QAAA,EAAA,EAAA,EAAA,CAAA;;4FAA1B,0BAA0B,EAAA,UAAA,EAAA,CAAA;kBAJtC,SAAS;AAAC,YAAA,IAAA,EAAA,CAAA;AACT,oBAAA,QAAQ,EAAE,qBAAqB;AAC/B,oBAAA,UAAU,EAAE;AACb,iBAAA;;0BAGsC;yCAWrC,SAAS,EAAA,CAAA;sBADR,YAAY;uBAAC,SAAS,EAAE,CAAC,QAAQ,CAAC;gBAInC,OAAO,EAAA,CAAA;sBADN,YAAY;uBAAC,OAAO,EAAE,CAAC,QAAQ,CAAC;;;ACtBnC;;AAEG;;ACFH;;AAEG;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ElementRef } from '@angular/core';
|
|
1
|
+
import { DoCheck, ElementRef } from '@angular/core';
|
|
2
2
|
import { NgControl } from '@angular/forms';
|
|
3
3
|
import * as i0 from "@angular/core";
|
|
4
|
-
export declare class AdrNumberValidateDirective {
|
|
4
|
+
export declare class AdrNumberValidateDirective implements DoCheck {
|
|
5
5
|
private el;
|
|
6
6
|
private control;
|
|
7
7
|
constructor(el: ElementRef, control: NgControl);
|
|
@@ -11,8 +11,10 @@ export declare class AdrNumberValidateDirective {
|
|
|
11
11
|
* Possible type of patterns allowed: X, X.X
|
|
12
12
|
*/
|
|
13
13
|
adrNumberValidate: import("@angular/core").InputSignal<string>;
|
|
14
|
+
private previousValue;
|
|
14
15
|
onKeyDown: (event: KeyboardEvent) => void;
|
|
15
16
|
onPaste: (event: ClipboardEvent) => void;
|
|
17
|
+
ngDoCheck(): void;
|
|
16
18
|
private checkValue;
|
|
17
19
|
private execute;
|
|
18
20
|
static ɵfac: i0.ɵɵFactoryDeclaration<AdrNumberValidateDirective, [null, { optional: true; }]>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dravishek/number-validate",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"angular",
|
|
6
|
+
"javascript",
|
|
7
|
+
"typescript",
|
|
8
|
+
"directive",
|
|
9
|
+
"input-number",
|
|
10
|
+
"number",
|
|
11
|
+
"number validation",
|
|
12
|
+
"validation",
|
|
13
|
+
"number-validate"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
4
16
|
"peerDependencies": {
|
|
5
17
|
"@angular/common": "^19.2.0",
|
|
6
18
|
"@angular/core": "^19.2.0"
|