-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented that stock in AddEditBookForm only allows whole numbers
- Loading branch information
1 parent
5221a05
commit 17dcd6f
Showing
5 changed files
with
35 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Directive, ElementRef, HostListener } from '@angular/core'; | ||
import { AbstractControl, NG_VALIDATORS } from '@angular/forms'; | ||
|
||
@Directive({ | ||
selector: '[appWholeNumber]', | ||
}) | ||
export class WholeNumberDirective { | ||
private regex: RegExp = new RegExp(/^[0-9]+$/); | ||
|
||
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', '-', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete']; | ||
|
||
constructor(private el: ElementRef) {} | ||
|
||
@HostListener('keydown', ['$event']) | ||
onKeyDown(event: KeyboardEvent) { | ||
// Allow Backspace, tab, end, and home keys | ||
if (this.specialKeys.indexOf(event.key) !== -1) { | ||
return; | ||
} | ||
|
||
let current: string = this.el.nativeElement.value; | ||
|
||
const position = this.el.nativeElement.selectionStart; | ||
|
||
const next: string = [current.slice(0, position), event.key == 'Whole' ? '.' : event.key, current.slice(position)].join(''); | ||
|
||
if (next && !String(next).match(this.regex)) { | ||
event.preventDefault(); | ||
} | ||
} | ||
} |