【问题标题】:How to validate the input field based on conditions in angular 6如何根据角度 6 中的条件验证输入字段
【发布时间】:2020-12-09 05:45:23
【问题描述】:

我有一个输入字段,它应该根据逻辑条件验证输入的文本。 它应该验证的条件是:-

1.公式中不能有空格。

2.操作符(and,or)必须小写并用{}括起来。

3.打开(和关闭)的次数将相等。

4.所有输入的task_id都应该出现在taskList数组中。

  1. 不应接受任何重复的 task_id。

示例:- (task_1{and}task_2){or}task_3

let taskList = ['task_1', 'task_2', 'task_3', 'task_4', 'task_5'];
let myModel = '';

onBlurMethod(){


}
<input type="text" [(ngModel)]="myModel" (blur)="onBlurMethod()">

我有一个包含 tasks_id 的数组,如果不满足条件,它应该会抛出错误。 我尝试使用正则表达式。

【问题讨论】:

  • 你的 sn-p 有错误

标签: javascript jquery angular regex


【解决方案1】:

您可以使用角度形式https://angular.io/guide/forms-overview

.component.html

    <form [formGroup]="loginForm" (ngSubmit)="onBlurMethod()">
    <nav for="userName" class="textinput">
        <input type="text" formControlName="myModel" class="form-control" />
    </nav>
    </form

.component.ts

  loginForm: FormGroup;
  constructor(
    private http: HttpClient,
    private locationService:LocationService,

    private formBuilder:FormBuilder
    ) { 
      this.loginForm = this.formBuilder.group({
        myModel: ['', Validators.required,Validators.pattern("")],
      });
    }
  onBlurMethod(){
    console.log(this.loginForm.value.myModel);
  }

Validators.pattern("")你可以写模式

因为,我不理解您的验证逻辑,但如果有任何疑问,请随时提出您的疑问。如果您在查询中添加更多信息,稍后我可以添加更多详细信息 谢谢你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2017-09-11
    • 2018-10-22
    • 1970-01-01
    • 2019-06-13
    • 2020-03-14
    • 2019-02-03
    相关资源
    最近更新 更多