使用Angular验证模糊的输入字段

时间:2014-08-07 09:16:47

标签: javascript jquery angularjs

我做了一个简单的测试输入字段,但是我试图转换模糊的验证,但我没有任何想法来实现这一点,因为我对angularjs并不熟悉。

任何人都帮我验证这个例子中只有一个模糊请...

myJs:

angular.module('myApp', [])
.controller('FormController', function($scope) {
  $scope.fields = [
    {placeholder: 'Username', isRequired: true},
    {placeholder: 'Password', isRequired: true},
    {placeholder: 'Email (optional)', isRequired: false}
  ];

  $scope.submitForm = function() {
    alert("it works!");
  };
});

HTML:

<div ng-app="myApp">
<form name="signup_form" ng-controller="FormController" ng-submit="submitForm()" novalidate>
  <div ng-repeat="field in fields" ng-form="signup_form_input">
    <input type="text"
           name="dynamic_input"
           ng-required="field.isRequired"
           ng-model="field.name"
           placeholder="{{field.placeholder}}" />
    <div ng-show="signup_form_input.dynamic_input.$dirty && signup_form_input.dynamic_input.$invalid">
      <span class="error" ng-show="signup_form_input.dynamic_input.$error.required">The field is required.</span>
    </div>
  </div>
  <button type="submit" ng-disabled="signup_form.$invalid">Submit All</button>
</form>
</div>

Live Demo

4 个答案:

答案 0 :(得分:17)

如果您更新为Angular 1.3,则可以使用ng-model-options更新模糊模型。

<input type="text"
       name="dynamic_input"
       ng-required="field.isRequired"
       ng-model="field.name"
       ng-model-options="{ updateOn: 'blur' }"
       placeholder="{{field.placeholder}}" />

fiddle

但是,如果您无法更新,那么有很多方法可以执行此操作here

答案 1 :(得分:11)

您只需创建一个与ngModel同名的指令即可。在元素上添加blur事件,将$dirty的{​​{1}}状态更改为true。通过在ngModel数组中添加回调,对元素进行更改时,请务必将ngModel的{​​{1}}状态更改为false。

<强> FORKED SAMPLE

该指令看起来像这样:

$dirty

注意:如果自定义指令名称ngModel.$viewChangeListeners与Angular的默认.directive('ngModel', function() { return { require: 'ngModel', link: function(scope, elem, attr, ngModel) { elem.on('blur', function() { ngModel.$dirty = true; scope.$apply(); }); ngModel.$viewChangeListeners.push(function() { ngModel.$dirty = false; }); scope.$on('$destroy', function() { elem.off('blur'); }); } } }); 相同,请不要担心,它会简单地运行它们(它赢了&# 39; t覆盖它)。 ngModel侦听器在范围被销毁时删除ngModel事件处理程序,例如当路由更改并且控制器被销毁或者移除字段时,会再次触发重复的DOM元素的重建(破坏scope.$on('$destroy')创建的子范围)。

答案 2 :(得分:3)

            angular.module('myApp', [])
        .controller('FormController', function($scope) {
             $(document).ready(function(){
                $("#form input").on('blur', function(){
                    alert("Cannot be null");
                });
            });
          $scope.fields = [
            {placeholder: 'Username', isRequired: true},
            {placeholder: 'Password', isRequired: true},
            {placeholder: 'Email (optional)', isRequired: false}
          ];

          $scope.submitForm = function() {
            alert("it works!");
          };

        });

只需为表单提供id并添加javascript代码即可。就这样。对于现场演示“http://jsfiddle.net/SaurabhGhewari/2ko9bamk/

答案 3 :(得分:1)

如果您有自定义验证,一个简单的方法是使用ngBlur。将以下内容添加到您的字段中并在控制器中编写验证函数:

<input type="text"
       ng-model="fieldVal" 
       ng-blur="validate(fieldVal)"/>

您还可以将ng-model-options与ng-change结合使用

<input type="text" 
       ng-modle="fildsVal" 
       ng-model-option="{ updateOn: 'blur' }" 
       ng-change="validate(fieldVal)" />

我还发现debounce选项非常适合触发验证。您可以计时,以便在用户完成输入时,验证触发:

<input type="text"
       ng-model-options="{ debounce: 800 }" 
       ng-pattern="/^[0-9]{1,9}$/" />
相关问题