-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathform.js
117 lines (104 loc) · 3.03 KB
/
form.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
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
// ajax to request saving form data into database and read the response
$(document).ready(function() {
$('#submit').on('click', function(e) {
e.preventDefault();
values = validateInfo();
if(values != ''){
var aj = $.ajax({
url: "book.php",
type: "POST",
data: values,
cache: false})
.done( function(dataResult){
try {
var dataResult = JSON.parse(dataResult);
}
catch (e) {
console.error('Could not parse JSON!');
}
if(dataResult.ok){
alert(dataResult.messages);
$( '.form' ).each(function(){
this.reset();
});
}
else {
alert(dataResult.messages);
}
})
.fail (function(){
alert("Error");
})
}
});
});
// function for validating form values
function checkItem(item,pattern){
var val = item;
return pattern.test(val);
}
// check if date is atleast two days from today
function checkDate(item){
var val = Date.parse(item);
var tomtom = new Date();
tomtom.setDate(tomtom.getDate()+2);
if(val >= tomtom){
return true;
}
else false;
}
function validateInfo(){
var ul = document.getElementById("form-errors");
var queryString = $('.form').serializeArray();
$('#form-errors').empty();
var values= {};
var ok = true;
//read input values into a dictionary and if any value is missing halt and return false
jQuery.each( queryString, function( i, field ) {
values[field.name] = field.value;
if(field.value == ""){
var li = document.createElement("li");
li.appendChild(document.createTextNode("Please fill all fields"));
ul.appendChild(li);
ok = false;
return ok;
}
});
var name = values['fname'] + " " + values['lname'];
//check if each field doesn't contain gibberish
if(ok){
if(!checkItem(name,/^[a-zA-Z-' ]*$/)){
var li = document.createElement("li");
li.appendChild(document.createTextNode("Invalid Name!"));
ul.appendChild(li);
ok = false;
}
if(!checkItem(values["phone"],/[01]{2}[0-2|5][0-9]{8}$/)){
var li = document.createElement("li");
li.appendChild(document.createTextNode("Invalid Phone Number!"));
ul.appendChild(li);
ok = false;
}
if(!checkItem(values["age"],/^\S[0-9]{0,2}$/)){
var li = document.createElement("li");
li.appendChild(document.createTextNode("Invalid Age!"));
ul.appendChild(li);
ok = false;
}
if(!checkItem(values["email"],/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/ )){
var li = document.createElement("li");
li.appendChild(document.createTextNode("Invalid Email!"));
ul.appendChild(li);
ok = false;
}
if(!checkDate(values["date"])){
var li = document.createElement("li");
li.appendChild(document.createTextNode("Invalid Date!"));
ul.appendChild(li);
ok = false;
}
}
// if inputs are valid return the values dictionary else return a blank string
if(ok) return values;
return '';
}