简体   繁体   中英

AngularJS Form validation Group error message

In angularJs FORM SUBMIT how to display all validation messagees in one single div at bottom of the page. Ex:

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

I referred documents but all are displaying below the text box using

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

How to write one area to bind all the messages in to same place (avoid use this userForm.email.$invalid )

Note I tried ng-message but am not clear any one help me. Please refer the below code for normal HMTL jQuery error display like need to take care using the 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 current code :

<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');
     }

    };
 }]);

How to rewrite?

If a form is invalid, the errors will be stored in the object $scope.form.$error . Please check console of this JSFiddle . From the $error property on the form, you can populate the error messages.

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

The 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM