簡體   English   中英

AngularJS表單驗證組錯誤消息

[英]AngularJS Form validation Group error message

在angularJs FORM SUBMIT中,如何在頁面底部的一個div中顯示所有驗證消息。 例如:

<div id="err-msg"> <!-- error message will come here --></div>

我提交了文檔,但是所有文檔都使用

<p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="alert alert-danger">Enter a valid email.</p>

如何編寫一個區域以將所有消息綁定到同一位置(避免使用此userForm.email.$invalid

注意我嘗試了ng-message但尚不清楚有人可以幫助我。 請參考以下代碼以顯示正常的HMTL jQuery錯誤,如需要使用AngularJS注意

的HTML

<div id="main">
<form> <div><input id="email_txt" type="text" name ="email" value="" > </div>
<div><input id="pass_txt" type="password" name ="password" value="" > </div>
<div><input id="validate_btn" type="submit" value="Validate" > </div>
</form>
</div>
<div id="err-msg"> <!-- error message will come here --></div>

jQuery的:

$('#main').on('click', '#validate_btn', function() { 
 if($('#email_txt').val().length == 0){
$('#err-msg').val("Please enter emailid!");
}else if($('#pass_txt').val().length == 0){
$('#err-msg').val("Please enter password!");
}
...
...
else{
  alert("Good");
}
});

AngularJS當前代碼:

<form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate>
<div class="form-group">
  <label>Username</label>
  <input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8">
   <p ng-show="userForm.username.$error.minlength" class="help-block">Username is too short.</p>
   <p ng-show="userForm.username.$error.maxlength" class="help-block">Username is too long.</p> </div> 
   <input type="email" name="email" class="form-control" ng-model="email">
   <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
   <button type="submit" class="btn btn-primary">Submit</button>
<form>

app.js

.controller('validatectrl',['$scope',function($scope){
     $scope.submitForm = function(isValid) {
     // check to make sure the form is completely valid
     if (isValid) {
         alert('Good');
     }

    };
 }]);

如何改寫?

如果表單無效,則錯誤將存儲在對象$scope.form.$error 請檢查此JSFiddle的控制台。 從窗體的$error屬性,您可以填充錯誤消息。

angular.module('Joy', [])
    .controller('FormCtrl', ['$scope', function ($scope) {
        $scope.user = {};
        $scope.submit = function () {
            console.log($scope.userInfo);
        };
    }]);

HTML:

<div ng-app="Joy" id="play-ground">
    <form name="userInfo" novalidate ng-controller="FormCtrl">
        <div class="ui input">
            <input ng-model="user.email" ng-message="YOU ARE WRONG" name="User Email" type="email">
        </div>
        <div class="ui input">
            <input ng-model="user.name" name="User Name" type="text" required>
        </div>
        <div class="ui divider"></div>
        <button class="ui primary button" ng-click="submit()">Submit</button>
        <div class="ui positive message">User: {{ user | json }}</div>
    </form>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM