简体   繁体   English

是否可以在AngularJS中使用双正则表达式(正则表达式)计算String?

[英]Is it possible to evaluate a String with a double regular expression (regex) in AngularJS?

I'm trying to evaluate a String against two simultaneous patterns in a personal ID text field (Fig. 1): 我正在尝试在个人ID文本字段中针对两个同时模式评估字符串(图1):

图。1

This field is intended for the user to input his personal Spanish (DNI) OR Portuguese (Cartão de cidadão) national identification number, and the idea is to evaluate both patterns at once, not caring about the locale set (the website is in Spanish, English and Portuguese). 该字段旨在供用户输入他的个人西班牙语(DNI)或葡萄牙语(Cartãodecidadão)国家识别号码,其目的是同时评估这两种模式,而不是关心语言环境集(网站是西班牙语,英语和葡萄牙语)。

Those patterns should be: 那些模式应该是:

  1. Spanish: /^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/i (ie 12345678A) 西班牙语: /^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/i (即12345678A)
  2. Portuguese: /^[0-9]{7,8}\\[0-9]\\([AZ]|[0-9]){2}[0-9]$/ (ie 04521225) 葡萄牙语: /^[0-9]{7,8}\\[0-9]\\([AZ]|[0-9]){2}[0-9]$/ (即04521225)

With this, the code snippet of the form is as follows: 有了这个,表单的代码片段如下:

<input type="text" name="field_dni"
    class="form-control form-control-400" id="field_dni"
    ng-model="vm.form.dni" 
    ng-minlength=9 
    ng-maxlength=9 
    ng-pattern= "vm.verificarId" />
<div ng-show="verificarForm.field_dni.$invalid">
    <p class="help-block"
        ng-show="verificarForm.field_dni.$error.pattern"
        translate="verificar.validation.pattern">The pattern entered is 
        incorrect
    </p>    
    <p class="help-block"
        ng-show="verificarForm.field_dni.$error.required"
        translate="verificar.validation.required">This field is required.
    /p>
    <p class="help-block"
        ng-show="verificarForm.field_dni.$error.minlength"
        translate="verificar.validation.minlength">This field is required to 
        be exactly 9 characters long
    </p>        
    <p class="help-block"
        ng-show="verificarForm.field_dni.$error.maxlength"
        translate="verificar.validation.maxlength">This field is required to 
        be exactly 9 characters long
    </p>

As you can see, what should happen is that every validation (length and pattern) are evaluated. 如您所见,应该发生的是每个验证(长度和模式)都要进行评估。

The relevant controller snippet looks like this: 相关的控制器代码段如下所示:

function ValidateController($scope, Principal, ValidateService,  $state) {
    var vm = this;
    vm.verificarId= /^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/i|/^[0-9]{7,8}\[0-9]\([A-Z]|[0-9]){2}[0-9]$/;

This does not work. 这不起作用。 The pattern seems to be ignored. 这种模式似乎被忽略了。 I have tried putting parentheses between the two regexp conditions, such as: 我试过在两个正则表达式条件之间加上括号,例如:

/(^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/i)|(/^[0-9]{7,8}\[0-9]\([A-Z]|[0-9]){2}[0-9]$)/

But throws a syntax error. 但抛出语法错误。 The only way I have managed to get this partially working is hardcoding either one regExp or the another into the ng-pattern component. 我设法使其部分工作的唯一方法是将一个regExp或另一个硬编码到ng-pattern组件中。 But this is not optimal, as both need to be working together. 但这并非最佳,因为两者都需要协同工作。

Is there a better and "Angularier" way of doing this? 这样做有更好的“Angularier”方式吗? I have tried to rebuild the regex again in regex101, with no success. 我试图在regex101中再次重建正则表达式,但没有成功。

I think that the problem is that you are using vm in the view. 我认为问题是你在视图中使用vm。 Assuming that you are using that controller in that view, the controller scope is the scope of the view, unless that you are using an alias 假设您在该视图中使用该控制器,则控制器范围是视图的范围,除非您使用别名

So you should be using ng-pattern= "verificarId" 所以你应该使用ng-pattern= "verificarId"

instate of ng-pattern= "vm.verificarId" ng-pattern= "vm.verificarId"

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

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