简体   繁体   English

Angular 6 中的字母数字字符串验证

[英]Alphanumeric String Validation in Angular 6

I need to validate user input with below validations and shoulw not allow user to submit the form untill the validation error messages are cleared我需要使用以下验证来验证用户输入,并且在清除验证错误消息之前不允许用户提交表单

Below are the validations my code need to perform.以下是我的代码需要执行的验证。 1)String will be a alphanumeric. 1)字符串将是一个字母数字。 2)String Length should not be more than 7 3)String should be either of theses patterns "w123456"(w followed by 6 numbers) or "df12345"(df followed by 5 numbers) 2) 字符串长度不应超过 7 3) 字符串应为“w123456”(w 后跟 6 个数字)或“df12345”(df 后跟 5 个数字)之一

Can someone help me how to validate this?有人可以帮助我如何验证这一点吗?

 Contact:   <textarea  required ngModel name="contacts" #contacts='ngModel' placeholder="Enter Valid contactid" ngModel name="contacts" #contacts="ngModel" rows="10" cols="15"></textarea>

I've provide a JSFiddle link below.我在下面提供了一个 JSFiddle 链接。 https://jsfiddle.net/sonyjammie/sx4rv8ne/4/ https://jsfiddle.net/sonyjammie/sx4rv8ne/4/

You can try to use compose validator of RxwebValidators for using multiple validators. 您可以尝试使用RxwebValidators的撰写验证器来使用多个验证器。 You can refer [ https://stackblitz.com/edit/angular-48jrxb?file=src%2Fapp%2Falpha-numeric-add.component.ts] for your reference. 您可以参考[ https://stackblitz.com/edit/angular-48jrxb?file=src%2Fapp%2Falpha-numeric-add.component.ts]作为参考。

Typically you would validate this with either some validate() function on the submit button, or alternatively, if using the RXJS library , you can assign an [array of Validators] as a second @parameter to a FormControl object. 通常,您可以使用提交按钮上的某些validate()函数来对此进行验证,或者,如果使用RXJS库 ,则可以将[验证者数组]作为第二个@parameter分配给FormControl对象。

Also check out the official documentation on Form Validation from Angular. 还可以从Angular上查看有关Form Validation的官方文档。

In Angular6, you would keep the form's submit button disabled by using the [disabled] attribute on the button element, and assigning your specific conditions to said element's [disabled] attribute. 在Angular6中,您可以通过使用button元素上的[disabled]属性,并将特定条件分配给该元素的[disabled]属性,来禁用表单的“提交”按钮。

name:['',[Validators.required,Validators.minLength(3),Validators.maxLength(15),Validators.pattern('[A-Za-z]')]],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM