[英]How to validate a text with a custom regex validation with return type as true or false in Angular 7
I am new to Angular development.我是 Angular 开发的新手。 I have the following requirements.
我有以下要求。 In case of text field, we need to validate the data entered in the text field with a regular expression with some extra validation.
在文本字段的情况下,我们需要使用带有一些额外验证的正则表达式来验证在文本字段中输入的数据。 In the .html file, the existing code is like this.
在.html文件中,现有的代码是这样的。
<input type="text" id="domainName" [pattern]="validateDomain()" [placeholder]="ComputeIPAddressMessages.DOMAIN_NAME_PLACEHOLDER" name="domainName" class="form-control"
[(ngModel)]="computeIPAddress.domainName" [disabled]="disabledComponent" #domainNameModel="ngModel"
size="20" required>
<span class="tooltip-content">
{{ComputeIPAddressMessages.INVALID_DOMAIN_NAME}}
</span>
In the corresponding .ts file, the code is like this.在对应的.ts文件中,代码是这样的。
public validateDomain(): any {
return CommonConstants.DOMAIN_NAME_REGEX;
}
There are few new requirements so that I have to strip the contents of the html text and I have to do some manipulations before performing regular expression match.几乎没有新要求,因此我必须剥离 html 文本的内容,并且必须在执行正则表达式匹配之前进行一些操作。
SO I want to write method like this.所以我想写这样的方法。
public validateDomain(): any {
// Check if the domain name already exists or not
// If the domain name start with .(period) or -(hyphen), remove it
// Other check
// perform some regular expression validation
// Return either true or false
return true or false;
}
Is it possible in case of Angular 7 ?在 Angular 7 的情况下有可能吗? Please suggest and help me to achieve it.
请建议并帮助我实现它。 This is the way I want to do custom regex validation instead of directly passing a regular expression.
这是我想要进行自定义正则表达式验证而不是直接传递正则表达式的方式。
It is not possible to give extra validations aside from regex patterns in [pattern] or pattern attribute in Angular as per my knowledge.据我所知,除了 [pattern] 中的正则表达式模式或 Angular 中的模式属性之外,不可能提供额外的验证。 The best way to handle it is using traditional (change) event in the html input tag and corresponding function call from component file.
处理它的最佳方法是在 html 输入标签中使用传统的(更改)事件以及来自组件文件的相应函数调用。 Add a boolean flag isInvalidDomain which will set to true when the pattern along with your conditions after doing your manipulations are not matching and set to false if validations are matching.
添加一个布尔标志isInvalidDomain ,当操作后的模式与条件不匹配时,该标志将设置为true ,如果验证匹配,则设置为false 。
sample.html示例.html
<input type="text" id="domainName" (change)="validateDomain()" [placeholder]="ComputeIPAddressMessages.DOMAIN_NAME_PLACEHOLDER" name="domainName" class="form-control" [(ngModel)]="computeIPAddress.domainName" [disabled]="disabledComponent" #domainNameModel="ngModel" size="20" required>
<span class="tooltip-content" *ngIf="isInvalidDomain"> {{ComputeIPAddressMessages.INVALID_DOMAIN_NAME}}
</span>
yourcomponent.ts你的组件.ts
isInvalidDomain :boolean = false;
validateDomain(){
if(condition1 == true){
if(computeIPAddress.domainName.test(CommonConstants.DOMAIN_NAME_REGEX)){
this.isInvalidDomain = false;
// success condition code
} else {
this.isInvalidDomain = true;
}
} else {
this.isInvalidDomain = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.