-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormValidationCode
More file actions
265 lines (244 loc) · 7.53 KB
/
Copy pathFormValidationCode
File metadata and controls
265 lines (244 loc) · 7.53 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Validation</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="dcterms.rightsHolder" content="Doug Bennett" />
<meta name="dcterms.dateCopyrighted" content="2017" />
<meta name="description" content="A short description of the page" />
<meta name="keywords" content="keywords describing this page, most search engines
ignore this now due to abuse"/>
<script src="validator.js"></script>
<script src="validatefunctions.js"></script>
<script>
//<![CDATA[
//Documenting your code
//with comments is a good thing.
//put you code here
//]]>
</script>
</head>
<body>
<h1> Doug's Form Validation Page </h1>
<form method="get" action="#">
<select name="stress" id="stressmenu"></select>
<span id="ddl-error">*</span>
<br>
<br />
<label for="name">Your Name: </label>
<input type="text" name="uname" id="name"><br>
<span id="name-error">*</span>
<br>
<br>
<label for="quantity"> Quantity: </label>
<input type="text" name="daquan" id="quantity"><br>
<span id="quantity-error">*</span>
<br>
<br>
<label for="shhpass"> Password: </label>
<input type="password" name="secret" id="shhpass"><br>
<span id="pass-error">*</span>
<br>
<br>
<fieldset id="bands">
<legend>Choose the World's Okayest band</legend>
<input type="radio" name="bandchoice" value="nickel"/> Nickelback <br/>
<input type="radio" name="bandchoice" value="wheeze"/> Weezer <br/>
<input type="radio" name="bandchoice" value="dave"/> Dave Matthews Band <br/>
<input type="radio" name="bandchoice" value="steve"/> Steve Miller Band <br/>
<input type="radio" name="bandchoice" value="charlotte"/> Good Charlotte <br/>
<input type="radio" name="bandchoice" value="oasis"/> Oasis <br/>
<input type="radio" name="bandchoice" value="papa"/> Papa Roach <br/>
<span id="band-choose-error">*</span>
</fieldset>
<fieldset id="schneider">
<legend>Choose your favorite Rob Schneider movie</legend>
<input type="radio" name="moviechoice" value="deuce"/> Deuce Bigalow <br/>
<input type="radio" name="moviechoice" value="animal"/> The Animal <br/>
<input type="radio" name="moviechoice" value="hotchick"/> The Hot Chick <br/>
<input type="radio" name="moviechoice" value="stapler"/> The Stapler <br/>
<input type="radio" name="moviechoice" value="carrot"/> A Carrot <br/>
<input type="radio" name="moviechoice" value="der"/> Der <br/>
<input type="radio" name="moviechoice" value="tump"/> Tump ta Tittly Tump Ta Too <br/>
<span id="schneider-choose-error">*<!--you must choose a movie....Or don't, it's your choice.--></span>
</fieldset>
<label for="sandcheck">Do you like Adam Sandler?</label>
<input type="checkbox" name="sandler" id="sandcheck" value="yes" checked="checked" />
<span id="sanderror"></span>
</br>
<p> What is your favorite thing about Adam Sandler other than Jack and Jill?</p>
<span id="remaining">*</span><br>
<textarea name="commenthere" cols="40" rows="20" id="favthings">
</textarea></br>
<input type="submit" name="submitted" value="Submit"></br>
<input type="reset" name="resetall" value="Reset"></br>
</form>
<div id="validator">
<a href="http://validator.w3.org/nu/?doc=https://nbtl.mesacc.edu/username/directories/filename.html">
HTML 5 Validation</a>
</div>
</body>
</html>
// validator.js
window.onload = createValidator;
function createValidator(linkId)
{
var myURL = window.location.href;
var validatorLink = '<a href="http://validator.w3.org/nu/?doc=';
validatorLink += myURL;
validatorLink += '">HTML 5 Validator</a>';
console.log(validatorLink);
document.getElementById('validator').innerHTML = validatorLink;
}
// validatefunctions.js
window.onload = function()
{
document.forms[0].onsubmit = checkfields;
createMenu(1, 10, 'On a scale from 1 to 10, how stressed are you?', 'stressmenu');
document.getElementById('stressmenu').onchange = stresscheck;
document.getElementById('name').onkeyup = fnamecheck;
document.getElementById('quantity').onkeyup = quantitycheck;
document.getElementById('shhpass').onkeyup = passcheck;
document.getElementById('bands').onclick = bandcheck;
document.getElementById('bands').onmouseover = bandcheck;
document.getElementById('schneider').onclick = schneidercheck;
document.getElementById('schneider').onmouseover = schneidercheck;
document.getElementById('sandcheck').onclick = sandlercheck;
document.getElementById('favthings').onkeyup = jackjilltextcheck;
//note if we can use event targets we can use ONE function
//but not today
}
function checkfields()
{
stresscheck();
fnamecheck();
quantitycheck();
passcheck();
bandcheck();
schneidercheck();
sandlercheck();
jackjilltextcheck();
return false;
}
function createMenu(start, end, mnuMsg, targetID)
{
var strMsg = '<option value="">' + mnuMsg + '</option>';
var counter = start;
while ( counter <= end)
{
strMsg += '<option value="' + counter + '">' + counter + '</option>';
counter++;
}
document.getElementById(targetID).innerHTML = strMsg;
}
function stresscheck()
{
var stressmenu = document.getElementById('stressmenu');
var stressSelect = stressmenu.options[stressmenu.selectedIndex].value;
if (stressSelect <= 10 && stressSelect >= 1)
{
strMsg = "Remember to breathe";
}
else
{
strMsg = "Please choose a number";
}
document.getElementById('ddl-error').innerHTML = strMsg;
}
function fnamecheck()
{
var fnamevar = document.getElementById('name').value;
if ( fnamevar.length < 2 || fnamevar.length > 25)
{
strMsg = "Name must be between 2 to 25 characters long";
}
else
{
strMsg= "OK";
}
document.getElementById('name-error').innerHTML = strMsg;
}
function quantitycheck()
{
var x, strMsg;
var x = document.getElementById('quantity').value;
if(isNaN(x) || x < 1)
{
strMsg = "enter numbers only dude";
}
else{
strMsg = "Cool!";
}
document.getElementById('quantity-error').innerHTML = strMsg;
}
function passcheck()
{
var passvar = document.getElementById('shhpass').value;
if ( passvar.length < 8 || passvar.length > 20)
{
strMsg = "Bro, your password needs to be between 8 and 20 Billion characters long (actually just 8 and 20 characters)";
}
else
{
strMsg = "Nice Password";
}
document.getElementById('pass-error').innerHTML = strMsg;
}
function bandcheck()
{
var bandItems = document.forms[0].bandchoice;
var strMsg = "it's not the end of the world if you don't pick a band, but the site won't work if you don't";
for (var i=0; i < bandItems.length; i++)
{
if(bandItems[i].checked == true)
{
strMsg = "Yeah, they're ok";
break;
}
}
document.getElementById('band-choose-error').innerHTML = strMsg;
}
function schneidercheck()
{
var schneiderItems = document.forms[0].moviechoice;
var strMsg = "Please pick a movie.....or don't it's your choice";
for (var i=0; i < schneiderItems.length; i++)
{
if(schneiderItems[i].checked == true)
{
strMsg = "Rated PG-13";
break;
}
}
document.getElementById('schneider-choose-error').innerHTML = strMsg;
}
function sandlercheck()
{
if (document.getElementById('sandcheck').checked == false)
{
strMsg = "yeah, he's not everyone's cup of tea";
}
else
{
strMsg = "";
}
document.getElementById('sanderror').innerHTML = strMsg;
}
function jackjilltextcheck()
{
var charsRemaining = 500 - document.getElementById('favthings').value.length;
if( document.getElementById('favthings').value.length > 500)
{
document.getElementById('favthings').value = document.getElementById('favthings').value.substring( 0,500);
}
else if ( document.getElementById('favthings').value.length < 500)
{
strMsg = charsRemaining + " characters remaining";
}
else
{
strMsg = "";
}
document.getElementById('remaining').innerHTML = strMsg;