简体   繁体   English

angular js中一个文本区域上的多个正则表达式模式

[英]Multiple regular expression pattern on one text area in angular js

I have a text area field where it inputs 4 types of personal details separated by commas. 我有一个文本区域字段,在其中输入以逗号分隔的4种个人详细信息。 I need to validate each of those 4 values using regular expression. 我需要使用正则表达式验证这四个值。 This is my approach but it is not much of the angular way to do the validations. 这是我的方法,但这并不是进行验证的多角度方法。

 var form = angular.module('form', []); form.controller('helloController', ['$scope', function($scope){ var submitted = false; var validNumber = new RegExp('numberRegExp'); var validArea = new RegExp('areaRegExp'); var validCity = new RegExp('cityRegExp'); $scope.submit = function(hello){ $scope.number = false; $scope.area = false; $scope.city = false; if (issue){ $scope.submitted = true; var splittedDetails = hello.details.split(","); var trimmedDetails = $.map(splittedDetails, $.trim); if (!validNumber.test(trimmedDetails[0])) { $scope.inputversion.jiraIssue.$invalid = true; $scope.number = true; }else if (!validArea.test(trimmedDetails[1])) { $scope.inputversion.jiraIssue.$invalid = true; $scope.area = true; }else if (!validCity.test(trimmedDetails[2])) { $scope.inputversion.jiraIssue.$invalid = true; $scope.city = true; }else{ alert("Form now submitting"); } } }; }]); 
 <form class="form-horizontal" name="helloForm" ng-controller="helloController" ng-submit="submit(hello)" novalidate ng-app="form"> <div class="form-group" ng-class="{ true:'has-error'}[submitted && helloForm.personal-details.$invalid]"> <label for="helloForm" class="col-sm-2 control-label">Details</label> <div class="col-sm-10"> <textarea class="form-control" rows="5" ng-model="hello.details" placeholder="number, area, city, details ex: 90********, XYX, XYZ" name="personal-details" required></textarea> <p ng-show="submitted && helloForm.personal-details.$error.required" class="help-block"> Details are required.</p> <p ng-show="submitted && number" class="help-block">Please check the format of issue number</p> <p ng-show="submitted && area" class="help-block">Please check the format of product area</p> <p ng-show="submitted && city" class="help-block">Please check the format of priority</p> </div> </div> </form> 

This approach can validate only one detail at a time. 这种方法一次只能验证一个细节。 But ideally it should validate dynamically in much of the angular way. 但是理想情况下,它应该以多种角度动态验证。 Please suggest your ideas. 请提出您的想法。 Thanks 谢谢

No offense, but this seems like a really bad way to validate 3 bits of expected data. 没有冒犯,但这似乎是验证3位预期数据的一种非常糟糕的方法。 Comma delimiting will only work if and only if the user puts 2 and only 2 commas into the field. 仅当用户在字段中输入2个和仅2个逗号时,逗号分隔才有效。

Assuming you want persisted data from the form (transfers from one page to another) you will want a Model for this module. 假设您要保留表单中的数据(从一页转移到另一页),则需要此模块的模型。 The best way to do this is via the .service (since there is only one instance of each named service in a module in Angular). 最好的方法是通过.service(因为Angular模块中每个命名服务只有一个实例)。 I'll also be using the format that will actually work with minified code and best practices, careof John Papa . 我还将使用实际上将与最小化代码和最佳实践一起使用的格式,请注意John Papa

It also assumes you have declared the module elsewhere (declare uses brackets [] while recall does not). 它还假定您已在其他位置声明了模块(声明使用方括号[],而调用未使用方括号[])。

angular.module('form')
  .service("DataService", DataService)
  DataService.$inject = ["$rootScope"];
  function DataService($rootScope) {
    var vm = this; //security in declaring (root)scope objects
    vm.phone = {value: '', placeholder: 'Enter your phone number'};
    vm.area = '';
    vm.city = '';
  };

Your controller would have little in it aside from the use of the $scope/$rootScope variables (with 2-way binding to fields and the Model) and your submit function for when the user tries to submit. 除了使用$ scope / $ rootScope变量(对字段和Model具有双向绑定)和用户尝试提交的Submit函数之外,您的控制器几乎没有其他用途。

angular.module('form')
  .controller('FormController', FormController);
  FormController.$inject = ['$scope', 'DataService'];
  function FormController($scope, DataService) {
    var vm = this;
    vm.phone = DataService.phone;
    vm.area = DataService.area;
    vm.city= DataService.city;
    function submit() {
    //persist the data to the next page/controller/etc.
    DataService.number = vm.number;
    DataService.area = vm.area;
    DataService.city = vm.city;
    //do work here
    };

You can do all of your validation within your HTML, using Angular's 2-way binding of data. 您可以使用Angular的数据两向绑定在HTML中进行所有验证。 Assuming you're also using BootStrap , each field should look similar to the below: 假设您还使用BootStrap ,则每个字段应类似于以下内容:

<!-- phone field (required/validated) -->
<div class="row form-group" ng-class="{'has-warning': helloForm.phone.$invalid && helloForm.phone.$dirty,'has-error': helloForm.phone.$error.required && helloForm.phone.$touched}">
<label for="phone" class="col-md-3 control-label">Phone&#58;</label>
<div class="col-md-9">
<input type="text" id="phone" name="phone" ng-model="vm.phone.value" placeholder="{{vm.phone.placeHolder}}" value="{{vm.phone.value}}" ng-required="true"class="skinny form-control" ng-pattern="/^\+?[-0-9)(]{8,31}[0-9x]{0,6}$/i" />
<p class="required" ng-show="helloForm.phone.$invalid || helloForm.phone.$error.required">&#42;</p>
<p class="good" ng-show="helloForm.phone.$valid">&#10004;</p>
</div>
<p class="help-block" ng-show="helloForm.phone.$error.pattern && helloForm.phone.$dirty">Phone format&#58; &#43;15558675309x52 or &#40;555&#41;867-5309x52</p>
<p class="help-block" ng-show="helloForm.phone.$error.required && helloForm.phone.$touched">Phone is a required field</p>
</div><br />

Rather than my E.164 (for international numbers) and American Standard phone combination validation (regex inside the ng-pattern field), you can use curly braces to declare your variable regex comparable. 除了我的E.164(用于国际号码)和美国标准电话组合验证(ng-pattern字段内的正则表达式)外,您还可以使用花括号来声明变量正则表达式具有可比性。 I hope this helps or at least points you in the right direction. 我希望这有助于或至少为您指明了正确的方向。

Thanks for reading, 谢谢阅读,

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

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