简体   繁体   English

如何在 Angular 7 中使用返回类型为 true 或 false 的自定义正则表达式验证来验证文本

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

相关问题 使用input [type =“ radio”]进行验证始终返回false - Validation with input[type=“radio”] return always false 在html中用javascript和php验证发布表单。一个错误返回true或false进入表单文本框 - In html posting a form with javascript and php validation.. one fault- return true or false goes into text box of form jQuery的True或False验证 - True Or False validation with jquery 如何使用angular或javascript基于true / false返回在html中显示值? - How do I use angular or javascript to display value in html based on true/false return? 需要根据文本字段中的值是否为特定数字来返回true或false - need to return true or false based on if value in text field is a specific number 如何使用jQuery Validation插件基于输入type = text / radio / checkbox验证表单 - How to validate form based on input type=text/radio/checkbox using jQuery Validation Plugin 如何使用正则表达式来验证jQuery中的文本框? - How to use regex to validate a text box in jquery? 表格验证;获取所有“输入类型=文本”并验证? - Form validation; Get all “input type=text” and validate? 如何解决此问题将始终在 Angular 中返回 false - how to fix this condition will always return false in Angular 输入类型为“ text”的angularjs自定义验证 - angularjs custom validation on input type=“text”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM