]> Dogcows Code - chaz/p5-CGI-Ex/blob - samples/validate_js_2_onchange.html
CGI::Ex 2.22
[chaz/p5-CGI-Ex] / samples / validate_js_2_onchange.html
1 <html>
2 <style>
3 .error {
4 color: red;
5 font-size: 75%;
6 }
7 </style>
8
9 <script src="../lib/CGI/Ex/validate.js"></script>
10 <script>
11 if (location.search) {
12 document.writeln('<span style="color:green"><h1>Form Information submitted</h1></span>');
13 }
14 if (! document.validate) {
15 document.writeln('<span style="color:red"><h1>Missing document.validate</h1>Path to ../lib/CGI/Ex/validate.js may be invalid.</span>');
16 } else {
17 document.writeln('<span style="color:green"><h1>Found document.validate</h1></span>');
18 }
19
20 </script>
21
22
23 <form name=a>
24 <table cellspacing=0 cellpadding=3>
25 <tr id=username_row>
26 <td valign=top>Username:</td>
27 <td><input type=text size=30 name=username></td>
28 <td id=username_img></td>
29 <td id=username_error class=error></td>
30 </tr>
31 <tr id=password_row>
32 <td valign=top>Password:</td>
33 <td><input type=password size=30 name=password><span id=password_strength style="font-size:smaller;color:blue"></span></td>
34 <td id=password_img></td>
35 <td id=password_error class=error></td>
36 </tr>
37 <tr id=password2_row>
38 <td valign=top>Verify Password:</td>
39 <td><input type=password size=30 name=password2></td>
40 <td id=password2_img></td>
41 <td id=password2_error class=error></td>
42 </tr>
43 <tr id=email_row>
44 <td valign=top>Email:</td>
45 <td><input type=text size=40 name=email></td>
46 <td id=email_img></td>
47 <td id=email_error class=error></td>
48 </td>
49 </tr>
50 <tr id=email2_row>
51 <td valign=top>Verify Email:</td>
52 <td><input type=text size=40 name=email2></td>
53 <td id=email2_img></td>
54 <td id=email2_error class=error></td>
55 </tr>
56 <tr id=state_row>
57 <td valign=top>State/Region:</td>
58 <td>
59 Specify State <input type=text size=2 name=state><br>
60 OR Region <input type=text size=20 name=region>
61 </td>
62 <td id=state_img></td>
63 <td id=state_error class=error></td>
64 </tr>
65 <tr id=enum_row>
66 <td valign=top>Enum Check:</td>
67 <td><input type=text size=10 name=enum></td>
68 <td id=enum_img></td>
69 <td id=enum_error class=error></td>
70 </tr>
71 <tr id=compare_row>
72 <td valign=top>Compare Check:</td>
73 <td><input type=text size=10 name=compare></td>
74 <td id=compare_img></td>
75 <td id=compare_error class=error></td>
76 </tr>
77 <tr id=checkone_row>
78 <td valign=top>Check one:</td>
79 <td>
80 <input type=checkbox name=checkone value=1> Foo<br>
81 <input type=checkbox name=checkone value=2> Bar<br>
82 <input type=checkbox name=checkone value=3> Baz<br>
83 </td>
84 <td id=checkone_img></td>
85 <td id=checkone_error class=error></td>
86 </tr>
87 <tr id=checktwo_row>
88 <td valign=top>Check two:</td>
89 <td>
90 <input type=checkbox name=checktwo value=1> Foo<br>
91 <input type=checkbox name=checktwo value=2> Bar<br>
92 <input type=checkbox name=checktwo value=3> Baz<br>
93 </td>
94 <td id=checktwo_img></td>
95 <td id=checktwo_error class=error></td>
96 </tr>
97 <tr><td colspan=2><hr></td></tr>
98 <tr id=foo_row>
99 <td valign=top>Fill In two:</td>
100 <td>
101 <input type=text name=foo value="" size=30> Foo<br>
102 <input type=text name=bar value="" size=30> Bar<br>
103 <input type=text name=baz value="" size=30> Baz<br>
104 </td>
105 <td id=foo_img></td>
106 <td id=foo_error class=error></td>
107 </tr>
108 <tr>
109 <td colspan=2 align=right>
110 <input type=submit value=Submit>
111 </td>
112 </tr>
113 </table>
114 </form>
115
116 <script src="../lib/CGI/Ex/validate.js"></script>
117 <script>
118 document.validate_set_hook = function (key) {
119 document.getElementById(key+'_img').innerHTML = '<span style="font-weight:bold;color:red">!</span>';
120 document.getElementById(key+'_row').style.background = '#ffdddd';
121 };
122 document.validate_clear_hook = function (key) {
123 document.getElementById(key+'_img').innerHTML = '<span style="font-weight:bold;color:green">+</span>';
124 document.getElementById(key+'_row').style.background = '#ddffdd';
125 };
126 document.validation = {
127 "group onevent": 'blur,change,submit',
128 "group no_confirm": 1,
129 "group no_alert": 1,
130 "group order": ["username", "password", "password2", "email", "email2", "state", "region", "s_r_combo", "enum", "compare", "checkone", "checktwo", "foo"],
131 username: {
132 name: "Username",
133 required: 1,
134 min_len: 3,
135 max_len: 30
136 },
137 password: {
138 name: "Password",
139 required: 1,
140 min_len: 6,
141 max_len: 30,
142 match: ["m/\\d/", "m/[a-z]/"],
143 match_error: "$name must contain both a letter and a number."
144 },
145 password2: {
146 name: "Verify password",
147 equals: "password",
148 equals_name: "password"
149 },
150 email: {
151 name: "Email",
152 required: 1,
153 max_len: 100,
154 type: 'email',
155 type_error: "$name must be a valid email address."
156 },
157 email2: {
158 name: "Verify email",
159 equals: "email",
160 equals_name: "email"
161 },
162 state: {
163 validate_if: ["state", "! region"],
164 match: "m/^\\w{2}$/",
165 match_error: "Please type a two letter state code."
166 },
167 region: {
168 validate_if: ["region", "! state"],
169 delegate_error: "state",
170 compare: "eq Manitoba",
171 compare_error: "For this test - the region should be Manitoba."
172 },
173 s_r_combo: {
174 field: "state",
175 delegate_error: "state",
176 max_in_set: "1 of state region",
177 max_in_set_error: "Specify only one of state and region.",
178 min_in_set: "1 of state region",
179 min_in_set_error: "Specify one of state and region."
180 },
181 'enum': {
182 name: "Enum check",
183 'enum': ["one", "two", "three", "four"],
184 enum_error: "$name must be one of one, two, three, or four."
185 },
186 compare: {
187 required: 1,
188 required_error: "Please type a number",
189 replace: "s/\\D//g",
190 name: "Compare check",
191 compare: ['> 99', '< 1000'],
192 compare_error: '$name must be greater than 99 and less than 1000.'
193 },
194 checkone: {
195 name: "Check one",
196 required: 1,
197 max_values: 1
198 },
199 checktwo: {
200 name: "Check two",
201 min_values: 2,
202 max_values: 2
203 },
204 foo: {
205 min_in_set: "2 of foo bar baz",
206 max_in_set: "2 of foo bar baz"
207 },
208 pw_strength: { // separate from the pw validation
209 field: "password",
210 custom_js: function (args) {
211 var v = args.value;
212 var n = 0;
213 if (v.match(/[a-z]/)) n++;
214 if (v.match(/[A-Z]/)) n++;
215 if (v.match(/[0-9]/)) n++;
216 var sym = v.match(/[ ~!@#$%^&*()_,.?{}\[\]]/) ? 1 : 0;
217 var s = (! v.length) ? ''
218 : (v.length < 3) ? 'weak'
219 : (v.length < 5) ? (sym || n == 3) ? 'ok' : 'weak'
220 : (v.length < 9) ? (n < 3 && ! sym) ? 'ok' : 'good'
221 : sym ? 'excellent' : 'good';
222 document.getElementById('password_strength').innerHTML = s;
223 // we could return false to indicate the strength wasn't good enough
224 return 1;
225 }
226 }
227 };
228 if (document.check_form) document.check_form('a');
229 </script>
230
231 </html>
232 <script>window.onload = function () { document.a.username.focus() }</script>
This page took 0.039109 seconds and 4 git commands to generate.