-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
59 lines (51 loc) · 1.48 KB
/
main.js
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
$(document).ready(function() {
var form = $('#form'),
name = $('#name'),
email = $('#email'),
message = $('#message'),
info = $('#info'),
loader = $('#loader'),
submit = $("#submit");
form.on('input', '#name, #email, #message', function() {
$(this).css('border-color', '');
info.html('').slideUp();
});
submit.on('click', function(e) {
info.html('Loading...').css('color', 'red').slideDown();
e.preventDefault();
if(validate()) {
$.ajax({
type: "POST",
url: "send.php",
data: form.serialize(),
dataType: "json"
}).done(function(data) {
if(data.success) {
name.val('');
email.val('');
message.val('');
info.html('Message sent!').css('color', 'green').slideDown();
} else {
info.html('Could not send mail! Sorry!').css('color', 'red').slideDown();
}
});
}
});
function validate() {
var valid = true;
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if($.trim(name.val()) === "") {
name.css('border-color', 'red');
valid = false;
}
if(!regex.test(email.val())) {
email.css('border-color', 'red');
valid = false;
}
if($.trim(message.val()) === "") {
message.css('border-color', 'red');
valid = false;
}
return valid;
}
});