跳到主要內容

Angularjs Form Validation

CRUD的網站中常具有登入、註冊的場景,頁面中可能會輸入一些值來做為登入與註冊的依據,但有些欄位可能需要一些智能的限制與提示,可以在前端先完成,而不用將整筆資料傳送到後端後再將驗證結果回傳,一來節省頻寬的浪費,二來可以即時將提示的樣板顯示於頁面,如email格式的檢查、密碼強度的限制等,而Angularjs也提供一些簡單的技巧來達成此功能,只要運用form搭配$valid、$error、$dirty等屬性即可,而於此也搭配ui-router來進行頁面的切換。


   一、首先設計index.html搭配bootstrap.css中的panel及ui-router的ui-view呈現register.html及register.message.html

<html ng-app="registerAppModule">
<head lang="en">
    <title></title>
    <link href="../../includes/bootstrap/css/bootstrap.css" rel="stylesheet">

    <script src="../../includes/jquery/jquery-2.1.1.min.js"></script>
    <script src="../../includes/angular/angular.min.js"></script>
    <script src="../../includes/angular/angular-ui-router.js"></script>

    <script src="../controller/registerApp.js"></script>
    <script src="../controller/registerController.js"></script>
    <script src="../../services/cacheMethod.js"></script>
</head>
<body>
<div class="panel panel-primary" align="center" style="width: 50% ;margin-left: auto;margin-right: auto;">
    <div class="panel-heading">Register</div>
    <div class="panel-body bg-warning" align="center" ui-view></div>
</div>
</body>
</html>

二、registerApp.js,用來設定ui-router狀態的設定與切換

(function () {
    angular.module('registerAppModule', ['ui.router','registerControllerModule'])
        .config(function($stateProvider,$urlRouterProvider,$locationProvider){
            $urlRouterProvider.otherwise("/register");
            $stateProvider.state('register',{
                url: '/register',
                templateUrl: 'register.html',
                controller: 'registerController'
            }).state('message',{
                url: '/message',
                templateUrl: 'register.message.html',
                controller: 'messageController'
            })
        })

})();

三、register.html,以ng-class、ng-show來切換驗證錯誤與驗證成功的樣板,而方法都置於registerController.js

<form name="registerForm" class="form-inline" ng-submit="submit(registerForm.$valid)" novalidate>
    <div class="form-group"  ng-class="getFormGroupClass(registerForm.idText)">
        <label  class="control-label" for='passwordText'>User Name:</label>
        <input type="text" class="form-control" id="idText" name="idText" placeholder="Enter your User Name..."  ng-model="idText" required>
        <span ng-show="registerForm.idText.$dirty" ng-class="getGlyphIconClass(registerForm.idText)"></span>
        <br />
        <label class="control-label" ng-show="isRequired(registerForm.idText)">*Required</label>
    </div>
    <p />

    <div class="form-group"  ng-class="getFormGroupClass(registerForm.passwordText)">
        <label class="control-label" for='passwordText'>Password:</label>
        <input type="password" class="form-control" id="passwordText" name="passwordText" placeholder="Enter your password..."  ng-model="passwordText" ng-pattern="passwordPattern" required>
        <span ng-show="registerForm.passwordText.$dirty" ng-class="getGlyphIconClass(registerForm.passwordText)"></span>
        <br />
        <label class="control-label" ng-show="isRequired(registerForm.passwordText)">*Required</label>
        <label class="control-label" ng-show="registerForm.passwordText.$error.pattern">*Required 8-12 characters with a-z and 0-9</label>
    </div>
    <p />
    <div   ng-class="getFormGroupClass(registerForm.emailText)">
        <label class="control-label" for='emailText'>Email:</label>
        <input type="email" class="form-control" id="emailText" name="emailText" placeholder="Enter your email..." ng-model="emailText" required/>
        <span ng-show="registerForm.emailText.$dirty" ng-class="getGlyphIconClass(registerForm.emailText)"></span>
        <br />
        <label class="control-label left" ng-show="isRequired(registerForm.emailText)">*Required</label>
        <label class="control-label left" ng-show="registerForm.emailText.$error.email">*Invalid email</label>
    </div>
    <p />
    <button type="submit" class="btn btn-info" ng-disabled="registerForm.$invalid">Submit</button>
</form>

四、registerController.js自訂filter為密碼增加mask,裡面具有兩個controller,分別為registerController來驗證資料的限制條件及設計submit function當驗證成功就將傳參的值存入cache並跳轉頁面,另外為messageController運用$viewContentLoaded的事件監聽來載入傳參資料並顯示。

(function(){
    angular.module('registerControllerModule',['ui.router','cacheMethodModule'])
        .filter('passwordFilter',function(){
            return function(str,symbol){//保留密碼的頭跟尾,中間字串取代成自訂符號
                if (!symbol) symbol = '*';
                if (!str) return;
                var fixShowLength = 2;
                var maskLength,replaceSourceStr,replaceTargetStr;
                if (str.length <= fixShowLength){
                    maskLength = str.length;
                    replaceSourceStr = str;
                }else{
                    maskLength = str.length - fixShowLength;
                    replaceSourceStr = str.substr(1,maskLength);
                }
               replaceTargetStr = '';
                for (var i = 0;i < maskLength;i++) replaceTargetStr += symbol;
                str = str.replace(replaceSourceStr,replaceTargetStr);
                return str;
            }
        })
        .controller('registerController',function($scope,$state,cacheMethodService){
            $scope.passwordPattern = /(?=^.{8,12}$)(?=.*\d)(?![.\n])(?=.*[a-z]).*$/;//配對8-12個英數混和字元
            $scope.submit = function(isValid){
                if (!isValid) return;
                cacheMethodService.saveCache('tmpParameter',{//傳參用
                    'username': $scope.idText,
                    'password': $scope.passwordText,
                    'email': $scope.emailText
                });
                $state.go('message');
            };
            $scope.getFormGroupClass = function(groupControls){
                if (!groupControls.$dirty) return;//若該Controls未修過值則不動作
                return groupControls.$invalid?'form-group has-error has-feedback':'form-group has-success has-feedback';//回傳驗證過與未驗證過的樣板
            }
            $scope.getGlyphIconClass = function(groupControls){
                return groupControls.$valid?'glyphicon glyphicon-ok form-control-feedback':'glyphicon glyphicon-remove form-control-feedback';//回傳驗證過與未驗證過的小圖示
            }
            $scope.isRequired = function(groupControls){
                return groupControls.$error.required && groupControls.$dirty;//判斷是否符合必填限制
            }
        })
        .controller('messageController',function($scope,cacheMethodService){
            $scope.$on('$viewContentLoaded', function () {//當頁面載入時讀取cache中傳參的值
                $scope.onLoad()
            });
            $scope.onLoad = function () {
                var cacheDatas = cacheMethodService.getCache('tmpParameter');
                $scope.userNameMessage = cacheDatas['username'];
                $scope.passwordMessage = cacheDatas['password'];
                $scope.emailMessage = cacheDatas['email'];

            }
        })
})();

五、cacheMethod.js存取快取資料

(function(){
    angular.module('cacheMethodModule',[])
        .service('cacheMethodService',function($cacheFactory){
            var cache = $cacheFactory('myCache');
            var saveCache = function(key,values){
                cache.put( key, values );
            }
            var getCache = function(key){
                var result =  cache.get(key);
                return !result?"":result;
            }
            return{
                saveCache: saveCache,
                getCache: getCache
            }
        })
})();

六、register.message.html

Your name is:{{userNameMessage}}
<br />
Your password is:{{passwordMessage|passwordFilter}}
<br />
Your email is :{{emailMessage}}
<p />
<a ui-sref="register" class="btn btn-info">go back</a>

留言

這個網誌中的熱門文章

java西元民國轉換_各種不同格式

C#資料庫操作(新增、修改、刪除、查詢)

【Excel好好玩】 自己的資產自己管!善用Google Sheet來幫我們評估貸款

這次介紹的主題是關於Excel的貸款還款計畫試算,我們人生中總會遇到需要大筆金額的花費,但當資金不夠時就得進行貸款,而貸款之前如果我們能夠審慎評估,並分析自己的還款能力之後在進行凍作,相信風險會小很多,因此就自己動動手來使用Google Sheet進行試算吧! 基本資料 ● 貸款總額: 1000000 ● 貸款期數: 84月 ● 年利率: 2.11% ● 月利率: 0.18% P.S 月利率 = 年利率 / 12 重要函式 PMT : 這是Google Sheet內建的重要年金計算公式,我們可以善用這個公式來計算固定利率及期數的固定攤還本息。因為PMT函式計算出的結果為負數,所以前面加上-號轉成正數。 動手做 首先我們在Excel表上列出我們的基本資料 圖片來源 其中月利率的部分就使用公式「=B4/12」 接著我們填上第一列的期數跟餘額 圖片來源 =B2 =B3 使用關鍵PMT函數來計算本息的部分 因為PMT函式計算出的結果為負數,所以前面加上-號轉成正數。 -PMT(貸款利率(月利率), 貸款期數, 貸款總額) =-PMT($B$5,$B$3,$B$2) 圖片來源 計算利息 利息 = 貸款餘額 x 月利率 =B8*$B$5 圖片來源 計算本金 本金 = 本息 - 利息 =C8-D8 圖片來源 製作第二列餘額的部分 餘額的部分 = 上一期的餘額 - 上一期的本金 圖片來源 接著拖曳該兩列往下拉,即可查看每一期的利息與本金 圖片來源 結語 雖然市面上已經有很多貸款銀行都提供了試算功能,但如果我們想要進一步管理自己的資產時,就需要將每一期的金額給計算出來,因此才會將公式運用在Excel表,讓我們的資產管理表能夠結合負債,進一步評估我們理財行動的下一步,希望這樣的經驗可以幫助到正在理財道路上打拼的夥伴,讓我們透過有效的管理,幫助荷包長大吧! 喜歡撰寫文章的你,不妨來了解一下: Web3.0時代下為創作者、閱讀者打造的專屬共贏平台 — 為什麼要加入? 歡迎加入一起練習寫作,賺取知識,累積財富!