How would I go about preventing the page from refreshing when pressing the send button without any data in the fields?
The validation is setup working fine, all fields go red but then the page is immediately refreshed. My knowledge of JS is relatively basic.
In particular I think the processForm()
function at the bottom is ‘bad’.
HTML
JavaScript
x
14
14
1
<form id="prospects_form" method="post">
2
<input id="form_name" tabindex="1" class="boxsize" type="text" name="name" placeholder="Full name*" maxlength="80" value="" />
3
<input id="form_email" tabindex="2" class="boxsize" type="text" name="email" placeholder="Email*" maxlength="100" value="" />
4
<input id="form_subject" class="boxsize" type="text" name="subject" placeholder="Subject*" maxlength="50" value="FORM: Row for OUBC" />
5
<textarea id="form_message" class="boxsize" name="message" placeholder="Message*" tabindex="3" rows="6" cols="5" maxlength="500"></textarea>
6
7
<button id="form_send" tabindex="5" class="btn" type="submit" onclick="return processForm()">Send</button>
8
<div id="form_validation">
9
<span class="form_captcha_code"></span>
10
<input id="form_captcha" class="boxsize" type="text" name="form_captcha" placeholder="Enter code" tabindex="4" value="" />
11
</div>
12
<div class="clearfix"></div>
13
</form>
14
JS
JavaScript
1
159
159
1
$(document).ready(function() {
2
3
// Add active class to inputs
4
$("#prospects_form .boxsize").focus(function() { $(this).addClass("hasText"); });
5
$("#form_validation .boxsize").focus(function() { $(this).parent().addClass("hasText"); });
6
// Remove active class from inputs (if empty)
7
$("#prospects_form .boxsize").blur(function() { if ( this.value === "") { $(this).removeClass("hasText"); } });
8
$("#form_validation .boxsize").blur(function() { if ( this.value === "") { $(this).parent().removeClass("hasText"); } });
9
10
11
12
///////////////////
13
// START VALIDATION
14
$("#prospects_form").ready(function() {
15
16
// DEFINE GLOBAL VARIABLES
17
var valName = $('#form_name'),
18
valEmail = $("#form_email"),
19
valEmailFormat = /^(([^<>()[]\.,;: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,}))$/,
20
valMsg = $('#form_message'),
21
valCaptcha = $('#form_captcha'),
22
valCaptchaCode = $('.form_captcha_code');
23
24
25
26
// Generate captcha
27
function randomgen() {
28
var rannumber = "";
29
// Iterate through 1 to 9, 4 times
30
for(ranNum=1; ranNum<=4; ranNum++){ rannumber+=Math.floor(Math.random()*10).toString(); }
31
// Apply captcha to element
32
valCaptchaCode.html(rannumber);
33
}
34
randomgen();
35
36
37
// CAPTCHA VALIDATION
38
valCaptcha.blur(function() {
39
function formCaptcha() {
40
if ( valCaptcha.val() == valCaptchaCode.html() ) {
41
// Incorrect
42
valCaptcha.parent().addClass("invalid");
43
return false;
44
} else {
45
// Correct
46
valCaptcha.parent().removeClass("invalid");
47
return true;
48
}
49
}
50
formCaptcha();
51
});
52
53
// Remove invalid class from captcha if typing
54
valCaptcha.keypress(function() {
55
valCaptcha.parent().removeClass("invalid");
56
});
57
58
59
// EMAIL VALIDATION (BLUR)
60
valEmail.blur(function() {
61
function formEmail() {
62
if (!valEmailFormat.test(valEmail.val()) && valEmail.val() !== "" ) {
63
// Incorrect
64
valEmail.addClass("invalid");
65
} else {
66
// Correct
67
valEmail.removeClass("invalid");
68
}
69
}
70
formEmail();
71
});
72
73
// Remove invalid class from email if typing
74
valEmail.keypress(function() {
75
valEmail.removeClass("invalid");
76
});
77
78
79
// VALIDATION ON SUBMIT
80
$('#prospects_form').submit(function() {
81
console.log('user hit send button');
82
83
// EMAIL VALIDATION (SUBMIT)
84
function formEmailSubmit() {
85
if (!valEmailFormat.test(valEmail.val())) {
86
// Incorrect
87
valEmail.addClass("invalid");
88
} else {
89
// Correct
90
valEmail.removeClass("invalid");
91
}
92
}
93
formEmailSubmit();
94
95
// Validate captcha
96
function formCaptchaSubmit() {
97
if( valCaptcha.val() === valCaptchaCode.html() ) {
98
// Captcha is correct
99
} else {
100
// Captcha is incorrect
101
valCaptcha.parent().addClass("invalid");
102
randomgen();
103
}
104
}
105
formCaptchaSubmit();
106
107
108
// If NAME field is empty
109
function formNameSubmit() {
110
if ( valName.val() === "" ) {
111
// Name is empty
112
valName.addClass("invalid");
113
} else {
114
valName.removeClass("invalid");
115
}
116
}
117
formNameSubmit();
118
119
120
// If MESSAGE field is empty
121
function formMessageSubmit() {
122
if ( valMsg.val() === "" ) {
123
// Name is empty
124
valMsg.addClass("invalid");
125
} else {
126
valMsg.removeClass("invalid");
127
}
128
}
129
formMessageSubmit();
130
131
132
// Submit form (if all good)
133
function processForm() {
134
if ( formEmailSubmit() && formCaptchaSubmit() && formNameSubmit() && formMessageSubmit() ) {
135
$("#prospects_form").attr("action", "/clients/oubc/row-for-oubc-send.php");
136
$("#form_send").attr("type", "submit");
137
return true;
138
} else if( !formEmailSubmit() ) {
139
valEmail.addClass("invalid");
140
return false;
141
} else if ( !formCaptchaSubmit() ) {
142
valCaptcha.parent().addClass("invalid");
143
return false;
144
} else if ( !formNameSubmit() ) {
145
valName.addClass("invalid");
146
return false;
147
} else if ( !formMessageSubmit() ) {
148
valMsg.addClass("invalid");
149
return false;
150
} else {
151
return false;
152
}
153
}
154
});
155
});
156
// END VALIDATION
157
/////////////////
158
});
159
Advertisement
Answer
You can prevent the form from submitting with
JavaScript
1
4
1
$("#prospects_form").submit(function(e) {
2
e.preventDefault();
3
});
4
Of course, in the function, you can check for empty fields, and if anything doesn’t look right, e.preventDefault()
will stop the submit.
Without jQuery:
JavaScript
1
4
1
var form = document.getElementById("myForm");
2
function handleForm(event) { event.preventDefault(); }
3
form.addEventListener('submit', handleForm);
4