Angular JS Magazine

marți, 2 septembrie 2014

Angular and Form Elements - part 7

In this tip you can see how to exploit some of The Attributes That Can Be Used for an input Element to fine grained the validation process:

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"/>
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet"/>
        angular.module("atpModule", [])
                .controller("atpCtrl", function ($scope) {

                    $scope.requireValue = true;
                    $scope.matchPattern = /([a-zA-Z\-']+){3,}\s+([a-zA-Z\-']+){3,}/;

                    $scope.addNewPlayer = function (newPlayer) {
                        if (atpForm.$valid) {
                            if (angular.isDefined(newPlayer)) {
                                     email:, rank: $scope.atp.length + 1});
                        } else {
                            $scope.validationStatus = true;
                    $scope.getErrorMessage = function (error) {
                        if (angular.isDefined(error)) {
                            if (error.required) {
                                return "Please provide a value";
                            } else if ( {
                                return "Please provide a valid e-mail address";
                            } else if (error.minlength) {
                                return "This value should be at least 10 characters";
                            } else if (error.maxlength) {
                                return "This value should be maximum of 25 characters";
                            } else if (error.pattern) {
                                return "This value should be of type 'name, surname (country)'";
        span.error {
            color: red;
<div ng-app="atpModule" ng-controller="atpCtrl" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', email: '', rank: 1 },
     { name: 'Djokovic, Novak (SRB)', email: '', rank: 2 },
     { name: 'Federer, Roger (SUI)', email: '', rank: 3 },
     { name: 'Wawrinka, Stan (SUI)', email: '', rank: 4 },
     { name: 'Ferrer, David (ESP)', email: '', rank: 5 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <tr ng-repeat="item in atp">
    <div class="col-xs-6">
        <form name="atpForm" novalidate ng-submit="addNewPlayer(player)">
            <div class="well">
                <div class="form-group row">
                    <label for="playerNameId">Player Name:</label>
                    <input id="playerNameId" name="playerName" type="text"
                           class="form-control" ng-model=""
                           ng-required="requireValue" ng-minlength="10" ng-maxlength="25"

                    <div ng-show="validationStatus">
                        <span class="error">{{getErrorMessage(atpForm.playerName.$error)}}</span>
                <div class="form-group row">
                    <label for="playerEmailId">Player E-mail:</label>
                    <input id="playerEmailId" name="playerEmail" type="email" class="form-control"
                           ng-model="" required>

                    <div ng-show="validationStatus">
                        <span class="error">{{getErrorMessage(atpForm.playerEmail.$error)}}</span>
                <div class="form-group row">
                    <label for="playerRankId">Player Rank:</label>
                    <span id="playerRankId">{{atp.length + 1}}</span>
                <button class="btn btn-primary btn-block">
                    Add Player