[英]Display error message if value pasted in input field whose type=“number”
In my form i have a quantity field which should accept only numeric values.在我的表单中,我有一个数量字段,它应该只接受数值。 When quantity is entered with special characters that time this type="number" replaces the special characters.
当使用特殊字符输入数量时,此 type="number" 将替换特殊字符。 But instead of replacing if i paste value with special characters i need to display error message as the "Quantity has invalid characters" and also should reset field value as "".
但是,如果我用特殊字符粘贴值而不是替换,我需要将错误消息显示为“数量有无效字符”,并且还应该将字段值重置为“”。
in html:在 html 中:
<mat-form-field class="quantity">
<mat-label>Item Quantity</mat-label>
<input matInput type="number" step="1" [(ngModel)]="quantity"
formControlName="itemQuantity" (paste)="checkItemQuantity($event)">
</mat-form-field>
in ts在 ts
checkItemQuantity(event){
let value = this.form.get('itemQuantity').value ; // if pasted value is 45%^$
console.log(value); // prints 45
}
as soon as i enter value with invalid characters in ts i am receiving value as 45 so i am unable to perform validation.一旦我在 ts 中输入带有无效字符的值,我就会收到 45 的值,所以我无法执行验证。 Is it possible to capture whatever value is pasted inside field.
是否可以捕获粘贴在字段中的任何值。 I couldnt use type="text" as i need step functionality for this field.
我不能使用 type="text" 因为我需要该字段的步骤功能。
You can try validating your FormControl inside your code (ie check if input is valid for type number).您可以尝试在代码中验证您的 FormControl(即检查输入是否对类型号有效)。 Then access the error message of the validation from the view, to get your user information.
然后从视图中访问验证的错误消息,以获取您的用户信息。
look here: Angular Form Validation看这里: Angular 表单验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.