forked from apoorvparijat/prashna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.html
215 lines (205 loc) · 6.74 KB
/
form.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
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
<html>
<head>
<title></title>
<style>
body {
margin: 0px;
}
.survey-question {
margin:10px;
padding:10px;
font-family:sans-serif;
color: #222;
display: none;
}
.survey-answer {
border:solid black 1px;
margin:10px;
padding:10px;
}
.survey-button {
font-size: 14px;
background-color: #49AD40 !important;
}
textarea {
width:100%;
}
.button {
display: inline-block;
padding: 6px 12px;
cursor: pointer;
margin-top: 5px;
color: #ffffff;
background-color: #407BAD;
border: 0px solid #357ebd;
border-radius: 2px;
}
.inner-question {
margin-bottom: 9px;
color: #555;
line-height: 1.4;
}
.inner-answer {
border-left: 2px solid #92C092;
padding-left: 10px;
margin-left: 10px;
margin-bottom: 15px;
color: #555;
line-height: 1.4;
}
.error {
color: red;
}
</style>
<script type='text/javascript'>
// Checks for the validation attributes and calls the respective validation functions.
//
// @param [DOM Object] el Input element
window.validate = function(el) {
if(el.attributes['required']) {
validateRequired(el);
}
if(el.attributes['no-space']) {
validateNoSpace(el);
}
if(el.attributes['email']) {
validateEmail(el);
}
}
// Validates if the field value is not set or is undefined or false
//
// @param [DOM Object] el Input element
window.validateRequired = function(el) {
var namespace = 'required'
if(el.value == '' || (el.type == 'checkbox' && el.checked == false)) {
window.showError(el, 'Field is required.', namespace);
} else {
window.hideError(el, namespace);
}
}
// Validates if the field value of the element has space
//
// @param [DOM Object] el Input element
window.validateNoSpace = function(el) {
var namespace = 'no-space';
if(el.value.match(/\s/)) {
window.showError(el, 'No space allowed.', namespace);
} else {
window.hideError(el, namespace);
}
}
// Validates if the email is correct
//
// @param [DOM Object] el Input element
window.validateEmail = function(el) {
var namespace = 'email';
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!el.value.match(re)) {
window.showError(el, 'Email is incorrect.', namespace);
} else {
window.hideError(el, namespace);
}
}
// Shows error message.
// Appends a span with the error text to the parent of the +el+
// if the span doesn't exist.
// If the span exists, it just sets its visibility.
//
// @param [DOM Object] el Input element
// @param [String] msg Error message
// @param [String] namespace A string representing the namespace of the error.
// Required to make sure the ID of the appended error
// element is unique.
window.showError = function(el, msg, namespace) {
var id_attr = 'error' + '-' + el.name + '-' + namespace;
var error_span = document.getElementById(id_attr);
if(error_span) {
error_span.style.display = 'block';
return;
}
appendErrorElement(el, msg, namespace);
}
// Hides the error message.
// Checks if the error element has been appended. If it is, it hides the element.
//
// @param [DOM Object] el Input element
// @param [String] namespace A string representing the namespace of the error.
// Required to make sure the ID of the appended error
// element is unique.
window.hideError = function(el, namespace) {
var id_attr = 'error' + '-' + el.name + '-' + namespace;
var error_span = document.getElementById(id_attr);
if(error_span) {
error_span.style.display = 'none';
return;
}
}
// Appends the error element to the parent of the +el+.
// Ideally, +el+ is the input field. If a validation fails,
// a span with error message is appended using this function.
//
// @param [DOM Object] el Input element
// @param [String] msg
// @param [String] namespace A string representing the namespace of the error.
// Required to make sure the ID of the appended error
// element is unique.
window.appendErrorElement = function(el, msg, namespace) {
var span = document.createElement('span');
var parent = el.parentElement;
span.innerHTML = msg;
span.id = 'error' + '-' + el.name + '-' + namespace;
span.className = 'error';
parent.appendChild(span);
}
// Show the question number
//
// @param [Integer] question_no
window.showQuestion = function(question_no) {
if(window.active) {
document.getElementById('q' + window.active).style.display = 'none';
}
window.active = question_no;
document.getElementById('q' + window.active).style.display = 'block';
}
window.active = 1;
</script>
</head>
<body>
<form name="prashnaform">
<div id='question-boxes'>
<div id="q1" class="survey-question" style='display:block'>
<div class="inner-question">Question 1: Are you male, female or something in between?</div>
<div class="inner-answer">I am<br>
<input type="radio" name="sex" value="male" checked> Male<br>
<input type="radio" name="sex" value="female"> Female<br>
<input type="radio" name="sex" value="female"> In-between
</div>
<div class='button' onclick='showQuestion(2)'>Next</div>
</div>
<div id="q2" class="survey-question">
<div class="inner-question">Question 2: What are your deepest fantasies and fetishes?</div>
<div class="inner-answer">
<textarea style="overflow:hidden" onkeyup='validate(this)' name='q2-answer' required>Let it all out</textarea>
</div>
<div class='button' onclick='showQuestion(3)'>Next</div>
</div>
<div id="q3" class="survey-question">
<div class="inner-question">Question 3: What is your email so we can send you all the info?</div>
<div class="inner-answer">
<input type="text" value="Here's my email" name='email' style="width:100%" onkeyup='validate(this)' email required no-space>
</div>
<div class='button' onclick='showQuestion(4)'>Next</div>
</div>
<div id="q4" class="survey-question">
<div class="inner-question">Question 4: What games do you enjoy playing?</div>
<div class="inner-answer">
<input type="checkbox" name="games" value="male"> FIFA<br>
<input type="checkbox" name="games" value="female"> Soul Calibur<br>
<input type="checkbox" name="games" value="female"> Pokemon
</div>
<input class="button survey-button" type="button" value="Submit this">
</div>
</div>
</form>
</body>
</html>