0

I am newbie in angularjs, and I have a question. I want to write only a directive for validate all types data (string, number,etc).

I wrote in JSFIDDLE,you can visit here: http://jsfiddle.net/wk8rbot5/5/, it worked.

But I dont want to use if-else to much in this file. I think we can use type or regex from other file. So anyone has suggestion, please let me know. Thank you so much.

var app = angular.module("app",[]);

app.controller("FormCtrl", function($scope) {   
  $scope.data = {};
  $scope.action = function() {
    debugger
    console.log($scope.data);
  }
});

app.directive("allowPattern", function() {
  return {
    restrict: "A",
    require: "ngModel",
    link: function(scope, el, attrs, ngModel) {
      var validator, patternValidator,
          validType = attrs.allowPattern,
          required = true;
      var pattern = "";
      if( validType =="email" ) {
        pattern = new RegExp(/^[A-Za-z0-9_-]+([\.]?[A-Za-z0-9_-])*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/);
      }
      else if(validType =="onlynumber"){
        //do other validate here
        pattern = new RegExp(/^[0-9]*$/);
      } else{
        parttern = new RegExp(/^.*S/); // !!! SyntaxError is fixed in this line
      }

      patternValidator = function(value) {
        return validate(pattern, value)
      };

      ngModel.$formatters.push(patternValidator);
      ngModel.$parsers.push(patternValidator);

      attrs.$observe("required", function(newval) {
        required = newval;
        patternValidator(ngModel.$viewValue);
      });

      function validate(regexp, value) {
        if( value == null || value === "" || !required || regexp.test(value) ) { // !!! TypeError: regexp.test is not a function 
          ngModel.$setValidity('pattern', true);
          return value;
        }
        else {
          ngModel.$setValidity('pattern', false);
          return;
        }
      }
    }
  };
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

<div ng-app="app" ng-controller="FormCtrl">
  <form name="theForm" novalidate="novalidate">
    <input type="text" ng-model="data.name" allow-pattern="email" required/>
    <button ng-disabled="theForm.$invalid" ng-click="action()">Action</button>
  </form>

  <br>
  other form:
  <br>
  <form name="theForm2" novalidate="novalidate">
    <input type="text" ng-model="data.number" allow-pattern="onlynumber" required/>
    <button ng-disabled="theForm2.$invalid" ng-click="action2()">Action</button>
  </form>

  <br>
  other form 3 :
  <br>
  <form name="theForm3" novalidate="novalidate">
    <input type="text" ng-model="data.abc" allow-pattern="" required/>
    <button ng-disabled="theForm3.$invalid" ng-click="action3()">Action</button>
  </form>
</div>
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
Ca Pham Van
  • 316
  • 1
  • 3
  • 12
  • Please, fix errors in your code. This code is not working. [mcve]. – Qwertiy Jun 06 '16 at 16:59
  • I wouldn't do this. It is not the angular/MVC way. Your programme should be modular and reusable (e.g., email validation should have no knowledge of number validation, etc.) It breaks testability of you combine them. – Kyle Jun 06 '16 at 17:02
  • Instead of reinventing the wheel, you'd better use tried & tested validation packages, like angular-ui-validate: https://github.com/angular-ui/ui-validate – Dmitriy Khudorozhkov Jun 06 '16 at 17:05
  • Take a look at validation directive implementation http://ru.stackoverflow.com/a/473299/178988 - it checks for fielsd equality. – Qwertiy Jun 06 '16 at 17:06

0 Answers0