如何验证 Angular 表单的主体是否以特定字符串开头和结尾。

huangapple go评论64阅读模式
英文:

How to validate if the body of the angular form starts and ends with particular string

问题

I've to take input from the user that starts with a particular string let's say XYZ and has business data in between and ends with ENDXYZ within the textview and I'm using angular framework for the UI

<div class="mb-3 col-md-5">
        <label for="bodyData" class="form-label">Body <span class="text-danger">*</span></label>
        <div class="wrapper">
          <textarea class="form-control" formControlName="bodyData"> </textarea>
        </div>
</div>

the ts part:

bodyData: ['',
        Validators.required
      ]

I thought I may use regex for the validation but I'm new to the angular framework so looking if there are any better ways to achieve it.

英文:

I've to take input from the user that starts with a particular string let's say XYZ and has business data in between and ends with ENDXYZ within the textview and I'm using angular framework for the UI

&lt;div class=&quot;mb-3 col-md-5&quot;&gt;
        &lt;label for=&quot;bodyData&quot; class=&quot;form-label&quot;&gt;Body &lt;span class=&quot;text-danger&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
        &lt;div class=wrapper&gt;
          &lt;textarea class=&quot;form-control&quot; formControlName=&quot;bodyData&quot;&gt; &lt;/textarea&gt;
        &lt;/div&gt;
&lt;/div&gt;

the ts part:

bodyData: [&#39;&#39;,
        Validators.required
      ]

I thought I may use regex for the validation but I'm new to the angular framework so looking if there are any better ways to achieve it.

答案1

得分: 1

使用内置的角度模式验证,你可以简单地这样做。

bodyData: ['',
[Validators.required, Validators.pattern(^XYZ.*\.XYZ$)]
]
英文:

You can simply do this using inbuilt angular pattern validation.

    bodyData: [&#39;&#39;, 
    [Validators.required, Validators.pattern(^XYZ.*\.XYZ$)]
    ]

答案2

得分: 1

Method 1:您可以直接检查内联:

 bodyData: ['',
        Validators.required, Validators.pattern(`your_pattern`)
      ]

Method 2: 您可以创建自定义验证器并检查您的情况:

在您的ts文件中:

import {MatchStringValidator} from './match-validator';
....
bodyData: ['',
        Validators.required
      ]

验证器:

import {Validator} from @angular/forms;

export class MatchStringValidator implements Validator {
    validate(string) {
     // 正则表达式逻辑以匹配您的条件
  }
英文:

Method 1:You can directly check inline:

 bodyData: [&#39;&#39;,
        Validators.required, Validators.pattern(`your_pattern`)
      ]

Method 2: You can create a custom validator and check for your scenario:

In your ts:

import {MatchStringValidator} from &#39;./match-validator&#39;;
....
bodyData: [&#39;&#39;,
        Validators.required
      ]

Validator:

import {Validator} from @angular/forms;

export class MatchStringValidator implements Validator {
    validate(string) {
     // regex logic to match your criteria
  }

答案3

得分: 0

你可能需要 https://angular.io/api/forms/Validators#pattern

你也可以编写自定义验证器。它只是一个带有单个控件参数的箭头函数,如果有错误,则返回一个包含错误的对象对象。

export declare interface ValidatorFn {
    (control: AbstractControl): ValidationErrors | null;
}
英文:

you probably want https://angular.io/api/forms/Validators#pattern

you can also write custom validators. it's just an arrow function with a single control argument, returning an object object with errors if any

export declare interface ValidatorFn {
    (control: AbstractControl): ValidationErrors | null;
}

huangapple
  • 本文由 发表于 2023年5月25日 18:06:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定