-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.js
57 lines (46 loc) · 1.2 KB
/
contact.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
// <!-- validate email -->
function checkEmail() {
var email = document.getElementById('myEmail');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}
// <!-- validate form -->
function validate(e){
var userName = document.getElementById("name").value;
var password = document.getElementById("message").value;
var errors = [];
if (!checkLength(userName,1,100)) {
errors[errors.length] = "What is your name?";
}
if (!checkLength(password,1,100)) {
errors[errors.length] = "What is your question?";
}
if (errors.length > 0) {
reportErrors(errors);
e.preventDefault();
}
}
function checkLength(text, min, max){
if (text.length < min || text.length > max) {
return false;
}
return true;
}
function reportErrors(errors){
var msg = "There were some problems...\n";
var numError;
for (var i=0; i<errors.length; i++) {
numError = i + 1;
msg += "\n" + numError + ". " + errors[i];
}
alert(msg);
}
window.onload = function() {
document.getElementById("contactForm").addEventListener("submit", function(e){
validate(e);
});
}