-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
54 lines (54 loc) · 2.05 KB
/
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
<form name="contact" method="POST" data-netlify="true">
<p>
<label>Your Name: <input type="text" id="name" name="name" /></label>
</p>
<p>
<label>Your Email: <input type="email" id="email" name="email" /></label>
</p>
<p>
<label>Your Role: <select name="role[]" multiple>
<option value="leader">Leader</option>
<option value="follower">Follower</option>
</select></label>
</p>
<p>
<label>Message: <textarea name="message"></textarea></label>
</p>
<p>
<button class="form_submit" type="submit">Send</button>
</p>
</form>
<script>
//get default border colours (to use on input when validation passes)
var borderStylePass = document.querySelector('#name').style.border;
//set fail border colours (to use on input when validation fails)
var borderStyleFail = '1px solid red';
//get the form submit button
var submit_button = document.querySelector('.form_submit');
//attach form event listener
submit_button.addEventListener("click", function(event){
//get the form "name" elemement
var name = document.querySelector('#name');
//get the form "email" element
var email = document.querySelector('#email');
//all validation is assumed to be passed until tested
blnValidated = true;
//change the border as it the validation passed
name.style.border = borderStylePass;
//if validation fails change the bln to false and change the input border colour
if(!name.value){
blnValidated = false;
name.style.border = borderStyleFail;
}
//if validation fails change the bln to false and change the input border colour
email.style.border = borderStylePass;
if(!email.value){
blnValidated = false;
email.style.border = borderStyleFail;
}
//if validation failed do not allow the form to submit the data
if(!blnValidated){
event.preventDefault();
}
}, false);
</script>