Strip some characters from pasted date input

This commit is contained in:
Michael Shamoon 2022-05-22 08:59:43 -07:00
parent feaf2da834
commit 8e2cb6d416
2 changed files with 13 additions and 2 deletions

View File

@ -2,7 +2,7 @@
<label class="form-label" [for]="inputId">{{title}}</label>
<div class="input-group" [class.is-invalid]="error">
<input class="form-control" [class.is-invalid]="error" [placeholder]="placeholder" [id]="inputId" maxlength="10"
(dateSelect)="onChange(value)" (change)="onChange(value)" (keypress)="onKeyPress($event)"
(dateSelect)="onChange(value)" (change)="onChange(value)" (keypress)="onKeyPress($event)" (paste)="onPaste($event)"
name="dp" [(ngModel)]="value" ngbDatepicker #datePicker="ngbDatepicker" #datePickerContent="ngModel">
<button class="btn btn-outline-secondary calendar" (click)="datePicker.toggle()" type="button">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-calendar" viewBox="0 0 16 16">

View File

@ -30,7 +30,18 @@ export class DateComponent
placeholder: string
// prevent chars other than numbers and separators
onPaste(event: ClipboardEvent) {
let clipboardData: DataTransfer =
event.clipboardData || window['clipboardData']
if (clipboardData) {
let pastedText = clipboardData.getData('text')
pastedText = pastedText.replace(/[\sa-z#!$%\^&\*;:{}=\-_`~()]+/g, '')
event.preventDefault()
this.value = pastedText
this.onChange(pastedText)
}
}
onKeyPress(event: KeyboardEvent) {
if ('Enter' !== event.key && !/[0-9,\.\/-]+/.test(event.key)) {
event.preventDefault()