简体   繁体   English

使用 angularjs 和 ngPattern 验证 UTF-8 名称

[英]Validate UTF-8 names with angularjs and ngPattern

Is there a way to use ng-pattern in form validation to validate any letter from any alphabet (latin, chinese, korean, russian, ...).有没有一种方法可以在表单验证中使用 ng-pattern 来验证任何字母表(拉丁文、中文、韩文、俄文等)中的任何字母。

I found a solution with the XRegExp library, but will not work with ng-pattern since it is expecting a string regex.我找到了一个使用 XRegExp 库的解决方案,但不适用于 ng-pattern,因为它需要一个字符串正则表达式。

XRegExp("^\\p{L}[\\p{L} ']*$")

One solution is too build my own validator directive, but I would prefer to use ng-pattern if possible.一种解决方案是构建我自己的验证器指令,但如果可能的话我更愿意使用 ng-pattern。

You can still use that regex in ng-pattern.您仍然可以在 ng-pattern 中使用该正则表达式。 You just have to pass a regex literal as a string like this:您只需要将正则表达式文字作为这样的字符串传递:

ng-pattern="/^\\p{L}[\\p{L} ']*$/"  
ng-pattern="/^.{3,20}$/"

allow any characters (include utf-8) and limit 3-20 characters, or允许任何字符(包括 utf-8)并限制为 3-20 个字符,或

ng-pattern="/^.[^*?&]{3,20}$/"

allow any characters and limit 4-20 character and except *?& (add more characters to except )允许任何字符并限制为 4-20 个字符,但 *?& 除外(向 except 添加更多字符)

I would suggest using a custom directive attribute, works fine for me.我建议使用自定义指令属性,对我来说很好用。 For example, below we are disallow Chinese characters for ngModel.例如,下面我们禁止 ngModel 使用中文字符。

function translationModelPatternDirective() {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function(scope, elem, attrs, ngModel) {
                if (!ngModel) {
                    return;
                }
                var regExp = XRegExp('\p{Han}');

                scope.$watch(function() {
                    return ngModel.$modelValue;
                }, function(newValue, oldValue) {
                    if (!regExp.test(newValue)) {
                        ngModel.$setViewValue(newValue);
                    } else {
                        ngModel.$setViewValue(oldValue);
                        ngModel.$render();
                    }
                });
            }
        };
    }

You need to add the u flag for Unicode support.您需要为 Unicode 支持添加u标志。 That way you can specify patterns that include character categories, such as \p{L} for a letter in any language, \p{M} for an accent mark in any language.这样,您可以指定包含字符类别的模式,例如\p{L}表示任何语言的字母, \p{M}表示任何语言的重音符号。

Example:例子:

ng-pattern="/^[\\p{L}\\p{M}][\\p{L}\\p{M}\\p{Zs}'\\-]*$/u"

Explanation:解释:

  • [\p{L}\p{M}] -- any letter in any alphabet, including accent mark [\p{L}\p{M}] -- 任何字母表中的任何字母,包括重音符号
  • \p{Zs} -- space separator in any language \p{Zs} -- 任何语言的空格分隔符
  • above pattern allows space, ' , and - at the end, you can tweak the pattern if you want to avoid that上面的模式允许空格、 '-最后,如果你想避免这种情况,你可以调整模式

See details on Unicode and character categories at https://javascript.info/regexp-unicode有关 Unicode 和字符类别的详细信息,请参阅https://javascript.info/regexp-unicode

Yes, you can use the ng-pattern directive in Angular forms to validate input containing letters from any alphabet.是的,您可以使用 Angular forms 中的 ng-pattern 指令来验证包含来自任何字母表的字母的输入。

To validate any letter from any alphabet, you can use the \p{L} character class in a regular expression.要验证来自任何字母表的任何字母,您可以在正则表达式中使用\p{L}字符 class。 The \p{L} character class matches any letter from any alphabet, including Latin, Chinese, Korean, Russian, and more. \p{L}字符 class 匹配任何字母表中的任何字母,包括拉丁文、中文、韩文、俄文等。

Here is an example of how to use the ng-pattern directive with the \p{L} character class to validate input that may contain letters from any alphabet:下面是一个示例,说明如何使用带有 \p{L} 字符 class 的 ng-pattern 指令来验证可能包含来自任何字母表的字母的输入:

<form name="myForm">
  <label for="name">Name:</label><br>
  <input type="text" id="name" name="name" ng-model="name" ng-pattern="/^[\p{L}]+$/"><br>
  <span ng-show="myForm.name.$error.pattern">Invalid name</span>
</form>

In this example, the ng-pattern directive is used to apply a regular expression to the name input field.在此示例中,ng-pattern 指令用于将正则表达式应用于名称输入字段。 The regular expression /^[\p{L}]+$/ allows any letter from any alphabet, and the ng-show directive is used to display an error message if the input does not match the pattern.正则表达式/^[\p{L}]+$/允许来自任何字母表的任何字母,并且 ng-show 指令用于在输入与模式不匹配时显示错误消息。

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

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