简体   繁体   English

如何处理多种语言的表单验证(特殊字符):AngularJS?

[英]How to handle form validation (special characters) for multiple languages : AngularJS?

consider I have below form which I want to validate for multiple languages. 考虑我有以下要验证多种语言的表格。

<form ng-submit="vm.transferCommunity()" name="transferCommunityForm">
<input type="text" class="form-control" name="transferCommunityLogin" ng-model="vm.transferCommunity.login" ng-pattern="/^[a-zA-Z0-9-_ ]+$/" >
<button type="submit" class="btn btn-default" ng-disabled="(transferCommunityForm.transferCommunityLogin.$invalid && !transferCommunityForm.transferCommunityLogin.$pristine)">Button</button>

<div ng-messages="transferCommunityForm.transferCommunityLogin.$error" ng-if="transferCommunityForm.transferCommunityLogin.$invalid">
  <div class="alert alert-danger" ng-message="pattern">Special Characters are not allowed</div>
</div>
</form>

The current pattern [ ng-pattern="/^[a-zA-Z0-9-_ ]+$/" ] by which I am validating my form. 我正在验证表单的当前模式[ ng-pattern="/^[a-zA-Z0-9-_ ]+$/" ]。 Its working fine for English language but for the languages like Japanese, Chinese, French, German , it displays error message. 它适用于英语,但适用于日语,中文,法语,德语等语言,并显示错误消息。

for example word Reference in English when translated to french becomes Référence and it clearly contains special characters but its valid. 例如,英语中的“ Reference一词翻译成法语Référence成为Référence ,它显然包含特殊字符,但有效。

How to solve these problem?? 如何解决这些问题?

Any help is appreciated !!! 任何帮助表示赞赏!

Thanks 谢谢

Instead of specifying all the correct values, you can specify all the wrong values. 您可以指定所有错误的值,而不是指定所有正确的值。

Create a regular expression that will contain all the wrong characters. 创建一个包含所有错误字符的正则表达式。

For example: /[@!#$%^&*()\\-+={}\\[\\]|\\\\/'";:.,~№?<>]+/i . 例如:/[ /[@!#$%^&*()\\-+={}\\[\\]|\\\\/'";:.,~№?<>]+/i ! /[@!#$%^&*()\\-+={}\\[\\]|\\\\/'";:.,~№?<>]+/i ;:.,~ /[@!#$%^&*()\\-+={}\\[\\]|\\\\/'";:.,~№?<>]+/i ?<>]+/ /[@!#$%^&*()\\-+={}\\[\\]|\\\\/'";:.,~№?<>]+/i

This regular expression will look for all the wrong characters. 此正则表达式将查找所有错误的字符。

Since ng-pattern expects us to give a positive regular expression, we need to override the execution ng-pattern . 由于ng-pattern希望我们给出一个正则表达式,因此我们需要重写ng-pattern的执行。

Live example on jsfiddle . jsfiddle上的实时示例。

 angular.module('ExampleApp', []) .controller('ExampleController', function($scope) { $scope.myPt = { regExp: /[@!#$%^&*()\\-+={}\\[\\]|\\\\/'";:`.,~№?<>]+/i, test: function(val) { console.log(val); return !this.regExp.test(val); } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <div ng-app="ExampleApp"> <div ng-controller="ExampleController"> <form name="myForm"> <input ng-model="firstName" name="firstName" ng-pattern="myPt" required> <br>firstName = <pre>{{myForm.firstName.$error|json}}</pre> </form> </div> </div> 

PS Maybe I forgot some wrong symbols. PS也许我忘记了一些错误的符号。 If need be, you can simply expand the regular expression. 如果需要,您可以简单地扩展正则表达式。

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

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