简体   繁体   English

如果值粘贴在 type=“number” 的输入字段中,则显示错误消息

[英]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.

相关问题 如果输入字段为空,则在输入字段旁边显示错误消息 - Display an error message next to the input field if input field is blank 如果在输入字段中输入了错误的类型,则在Chrome中显示消息 - Display message in Chrome if wrong type is entered in input field Javascript-检查表单字段值,如果value为0,则显示错误消息 - Javascript - check form field value and display error message if value is 0 绑定成角度时,input [number]字段不显示字符串值 - input[number] field does not display string value while binding in angular 如何获得原始值<input type="number">场地? - How to get the raw value an <input type="number"> field? 这是使用值替换更新输入类型=数字字段的预期行为 - Which is the intended behaviour on update of input type=number field with value replace Safari输入类型=“数字”无法显示实际值 - safari input type='number' can't display real value 如何在旁边的跨度中显示“数字类型输入”的值 - How to display the value of a “number type input” in a span next to it 在javascript的输入数字类型字段中输入值时如何显示输入文本字段? - How to show a input text field when a value is entered in input number type field in javascript? 检测输入字段中的错误信息 - Detect the error message in the input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM