2016-11-10 3 views
0

Ich habe eine Cordova Ionic App erstellt. Ich habe unten Code versucht. Die Direktive funktioniert in iOS aber nicht in Android. Es ermöglicht dem Benutzer, Sonderzeichen einzugeben. Kann mir bitte jemand sagen, wie ich das lösen kann.Mobilnummer Validierung in Ionic Android

register.html

    <label class="item item-input"> <span><i 
          class="icon ion-ios-telephone-outline"></i></span> <input type="number" 
         ng-pattern="/^[0-9]{10,10}$/" placeholder="Mobile" only-num 
         ng-model="register.mobile_number" name="mobile" ng-minlength="10" 
         ng-maxlength="10" required> 
        </label> 

app.js

  app.directive('onlyNum', function() { 
       return function(scope, element, attrs) { 

        var keyCode = [8,9,37,39,48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,110]; 
        element.bind("keydown", function(event) { 
         console.log($.inArray(event.which,keyCode)); 
         if($.inArray(event.which,keyCode) == -1) { 
          scope.$apply(function(){ 
           scope.$eval(attrs.onlyNum); 
           event.preventDefault(); 
          }); 
          event.preventDefault(); 
         } 

        }); 
       }; 
      }); 

ich unten Richtlinie versucht haben, auch:

register.html

    <label class="item item-input"> <span><i 
          class="icon ion-ios-telephone-outline"></i></span> <input type="number" 
         ng-pattern="/^[0-9]{10,10}$/" placeholder="Mobile" allow-pattern="\d" 
         ng-model="register.mobile_number" name="mobile" ng-minlength="10" 
         ng-maxlength="10" required> 
        </label> 

app.js

app.directive('allowPattern', [allowPatternDirective]); 

      function allowPatternDirective() { 
       return { 
        restrict: "A", 
        compile: function(tElement, tAttrs) { 
         return function(scope, element, attrs) { 
          // I handle key events 
          element.bind("keypress", function(event) { 
           var keyCode = event.which || event.keyCode; // I safely get the keyCode pressed from the event. 
           var keyCodeChar = String.fromCharCode(keyCode); // I determine the char from the keyCode. 

           // If the keyCode char does not match the allowed Regex Pattern, then don't allow the input into the field. 
           if (!keyCodeChar.match(new RegExp(attrs.allowPattern, "i"))) { 
            event.preventDefault(); 
            return false; 
           } 

          }); 
         }; 
        } 
       }; 
      }; 

Antwort

0

Bitte diese Anweisung verwenden es nur Zahlen erlauben

app.directive('numbersOnly', function() { 

    return { 
     restrict: 'A', 
     require: '?ngModel', 
     link: function (scope, element, attrs, modelCtrl) { 
      modelCtrl.$parsers.push(function (inputValue) { 
       if (inputValue == undefined) 
        return ''; 
       var transformedInput = inputValue.replace(/[^0-9]/g, ''); 
       if (transformedInput !== inputValue) { 
        modelCtrl.$setViewValue(transformedInput); 
        modelCtrl.$render(); 
       } 
       return transformedInput; 
      }); 
     } 
    }; 
}) 

und bitte diese Anweisung in Ihrer Eingabe

<input type="text" numbers-only> 

diese eingereicht sind funktioniert gut für mich

+0

Hat es auch in Android funktioniert? –

+0

Funktioniert nicht in meinem Fall ... Könnten Sie bitte andere Lösung bieten ... –

+0

Bitte Nummern einfügen - nur als Direktive im Eingabefeld wird dies funktionieren –