2017-05-08 67 views
1

提交按鈕的形式應該被禁用,直到所有的字段被填充,但我注意到,當用戶輸入用戶名的按鈕來活着。angularjs-禁用提交,直到所有字段填充

<div class="list"> 
<form id="create" name="pw" method="post"> 
    <label class="item item-input item-stacked-label"> 
    <span class="input-label">Username</span> 
    <input type="text" placeholder="kojobaah" ng-model="username" required> 
</label> 

    <label for="password">New Password 
    <input type="password" name="user_password" ng-model="user_password" ng-required="confirm_password && !user-password" password-verify="confirm_pasword"> 
    <p ng-show="pw.user_password.$error.passwordVerify">Passwords do not match</p> 
    <p ng-show="pw.user_password.$error.required">This field is required</p> 
    </label> 
</p> 
<p> 
    <label for="password">Confirm Password 
    <input type="password" name="confirm_password" ng-model="confirm_password" ng-required="user_password && !confirm_password" password-verify="user_password"> 
    <p ng-show="pw.confirm_password.$error.passwordVerify">Passwords do not match</p> 
    <p ng-show="pw.confirm_password.$error.required">This field is required</p> 
    </label> 
    <br /> 
    <p align="center"><button ng-disabled="create.$invalid" class="button button-balanced" ng-click="register()">Create Account</button></p> 
    </form> 
    </div> 

JS

.directive('passwordVerify', function() { 
    return { 
     restrict: 'A', 
     require: '?ngModel', 
     link: function(scope, elem, attrs, ngModel) { 
      scope.$watch(attrs.ngModel, function() { 
       if (scope.confirm_password === scope.user_password) { 
        scope.pw.confirm_password.$setValidity('passwordVerify', true); 
        scope.pw.user_password.$setValidity('passwordVerify', true); 
       } else if (scope.confirm_password !== scope.user_password) { 
        scope.pw.confirm_password.$setValidity('passwordVerify', false); 
        scope.pw.user_password.$setValidity('passwordVerify', false); 
       } 
      }); 
     } 
    }; 
}) 

如何使禁用,直到用戶名填寫和密碼一致的按鈕任何幫助嗎?

回答

2

您的表單名稱爲pw,因此您需要深入瞭解Angular的表單整體有效性指示的表單。

簡單的改變:

<button ng-disabled="create.$invalid" 
     class="button button-balanced" 
     ng-click="register()"> 
     Create Account 
</button> 

到:

<button ng-disabled="pw.$invalid" 
     class="button button-balanced" 
     ng-click="register()"> 
     Create Account 
</button> 

工作Plunker:http://plnkr.co/edit/0zlN2TmEIbGQW5Mq7YJ0?p=preview

更新:切換ng-required簡單的密碼字段true和移動按鈕上的ng-disabled指令的比較邏輯實現了所需的驗證檢查。

<button ng-disabled="pw.$invalid || (user_password != confirm_password)" 
     class="button button-balanced" 
     ng-click="register()"> 
     Create Account 
</button> 
+0

只是測試你的答案,但在輸入密碼之前,該按鈕被激活 – user6579134

+0

這是因爲驗證檢查只是填充了輸入,而不是密碼A與密碼B匹配。 – couzzi

+0

你也可以幫助使用密碼嗎? – user6579134

1

由於您的窗體的名稱是PW,只需更改您的創建帳戶按鈕:

<button ng-disabled="pw.$invalid" 
     class="button button-balanced" 
     ng-click="register()"> 
     Create Account 
</button> 
+0

我這樣做了,但是隻填寫用戶名文本框時,該按鈕變爲活動狀態。它應該保持禁用,直到用戶名被填寫並且密碼匹配 – user6579134

相關問題