article

Wednesday, January 19, 2022

AngularJS Form Validation

AngularJS Form Validation

angularjs CDN
https://angularjs.org/ Version 1.8.2 https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//index.html
<!DOCTYPE html>
<html lang="en" ng-app="formvalidation">
<head>
<meta charset="utf-8">
<title>AngularJS Form Validation</title>
<style type="text/css">
.errortext{
    color:red;
}
</style>
</head>
<body ng-controller="cntform">
<h1 class="page-header text-center">AngularJS Form Validation</h1>
<div class="signup-form">
    <form role="form" name="myForm" novalidate>
        <h2>Register</h2>
        <p class="hint-text">Create your account. It's free and only takes a minute.</p>
        <div class="form-group">
            <input type="text" class="form-control" name="username" placeholder="Username" ng-minlength="10" ng-maxlength="30" ng-pattern="/^[a-zA-Z0-9_]*$/" ng-model="user.username" required autofocus>
            <div class="errortext" ng-show="myForm.username.$dirty && myForm.username.$invalid">
                <span ng-show="myForm.username.$error.required">Username is required</span>
                <span ng-show="myForm.username.$error.minlength">Username must contain atleast 10 characters</span>
                <span ng-show="myForm.username.$error.maxlength">Username should not be greater than 30 characters</span>
                <span ng-show="myForm.username.$error.pattern && !myForm.username.$error.minlength && !myForm.username.$error.maxlength">Only letters, numbers and underscore allowed</span>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-xs-6">
                    <input type="text" class="form-control" name="firstname" placeholder="Firstname" ng-model="user.firstname" required>
                    <div class="errortext" ng-show="myForm.firstname.$dirty && myForm.firstname.$invalid">
                        <span ng-show="myForm.firstname.$error.required">Firstname is required</span>
                    </div>
                </div>
                <div class="col-xs-6">
                    <input  type="text" class="form-control" name="lastname" placeholder="Lastname" ng-model="user.lastname" required>
                    <div class="errortext" ng-show="myForm.lastname.$dirty && myForm.lastname.$invalid">
                        <span ng-show="myForm.lastname.$error.required">Lastname is required</span>
                    </div>
                </div>
            </div>           
        </div>
        <div class="form-group">
            <input  type="email" class="form-control" name="email" placeholder="Email" ng-model="user.email" required>
            <div class="errortext" ng-show="myForm.email.$dirty && myForm.email.$invalid">
                <span ng-show="myForm.email.$error.required">Email is required</span>
                <span ng-show="myForm.email.$error.email">Invalid email format</span>
            </div>
        </div>       
        <div class="form-group">
            <input type="url" class="form-control" name="url" placeholder="Website" ng-model="user.website" required>
            <div class="errortext" ng-show="myForm.url.$dirty && myForm.url.$invalid">
                <span ng-show="myForm.url.$error.required">Website is required</span>
                <span ng-show="myForm.url.$error.url">Input a valid website</span>
            </div>
        </div>
        <div class="form-group">
            <input type="password" class="form-control" name="password" placeholder="Password" ng-model="user.password" required>
            <div class="errortext" ng-show="myForm.password.$dirty && myForm.password.$invalid">
                <span ng-show="myForm.password.$error.required">Password is required</span>
            </div>
        </div>
        <div class="form-group">
            <input type="password" class="form-control" name="repassword" placeholder="Re-type Password" ng-model="user.repassword" required>
            <div class="errortext" ng-show="myForm.repassword.$dirty && myForm.repassword.$invalid || myForm.repassword.$dirty && user.repassword != user.password">
                <span ng-show="myForm.repassword.$error.required">Re-type password is required</span>
                <span ng-show="!myForm.repassword.$error.required && user.repassword != user.password">Password did not match</span>
            </div>
        </div>       
        <div class="form-group">
            <label class="checkbox-inline"><input type="checkbox" required="required"> I accept the <a href="#">Terms of Use</a> & <a href="#">Privacy Policy</a></label>
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-success btn-lg btn-block" ng-disabled="myForm.$invalid || user.repassword != user.password" ng-click="submit()"><span class="glyphicon glyphicon-check"></span> Register Now</button>
        </div>
         
        <div class="alert alert-success text-center" ng-show="valid">
            <button type="button" class="close" ng-click="close()"><span aria-hidden="true">×</span></button>
            <span class="glyphicon glyphicon-check"></span> Form Validated
        </div>
         
    </form>
    <div class="text-center">Already have an account? <a href="#">Sign in</a></div>
</div>
<script>
var app = angular.module('formvalidation', []);
app.controller('cntform', function($scope){
    $scope.valid = false;
    $scope.submit = function(){
        $scope.valid = true;
    }
    $scope.close = function(){
        $scope.valid = false;
    }
});
</script>
<style>
    body {
        color: #fff;
        background: #63738a;
        font-family: 'Roboto', sans-serif;
    }
    .form-control{
        height: 40px;
        box-shadow: none;
        color: #969fa4;
    }
    .form-control:focus{
        border-color: #5cb85c;
    }
    .form-control, .btn{       
        border-radius: 3px;
    }
    .signup-form{
        width: 400px;
        margin: 0 auto;
        padding: 30px 0;
    }
    .signup-form h2{
        color: #636363;
        margin: 0 0 15px;
        position: relative;
        text-align: center;
    }
    .signup-form h2:before, .signup-form h2:after{
        content: "";
        height: 2px;
        width: 30%;
        background: #d4d4d4;
        position: absolute;
        top: 50%;
        z-index: 2;
    }  
    .signup-form h2:before{
        left: 0;
    }
    .signup-form h2:after{
        right: 0;
    }
    .signup-form .hint-text{
        color: #999;
        margin-bottom: 30px;
        text-align: center;
    }
    .signup-form form{
        color: #999;
        border-radius: 3px;
        margin-bottom: 15px;
        background: #f2f3f7;
        box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
        padding: 30px;
    }
    .signup-form .form-group{
        margin-bottom: 20px;
    }
    .signup-form input[type="checkbox"]{
        margin-top: 3px;
    }
    .signup-form .btn{       
        font-size: 16px;
        font-weight: bold;     
        min-width: 140px;
        outline: none !important;
    }
    .signup-form .row div:first-child{
        padding-right: 10px;
    }
    .signup-form .row div:last-child{
        padding-left: 10px;
    }      
    .signup-form a{
        color: #fff;
        text-decoration: underline;
    }
    .signup-form a:hover{
        text-decoration: none;
    }
    .signup-form form a{
        color: #5cb85c;
        text-decoration: none;
    }  
    .signup-form form a:hover{
        text-decoration: underline;
    
</style>
</body>
</html>

Related Post