-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdmin.js
98 lines (88 loc) · 3.27 KB
/
Admin.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
$(document).ready(function () {
$("form").submit(function (event) {
$(".help-block").remove();
var formData = {
CarID : $("#CarID").val(),
StartDate: $("#StartDate").val(),
EndDate: $("#EndDate").val(),
StateDate: $("#StateDate").val(),
};
// if Car ID is not Empty check if it is not a number
if (isNaN(formData["CarID"])) {
alert("Please enter a Valid Plate ID");
}
// if start date is not Empty
if (formData["StartDate"] != "") {
validatedate(formData["StartDate"]);
}
// if End date is not Empty
if (formData["EndDate"] != "") {
validatedate(formData["EndDate"]);
}
// if State date is not Empty
if (formData["StateDate"]) {
validatedate(formData["StateDate"]);
}
$.ajax({
type: "POST",
url: "Admin.php",
data: formData,
dataType: "json",
encode: true,
}).done(function (data) {
console.log(data);
if (!data.success) { // Error
if (data.errors.email) { // If email already exists
$("#email-group").addClass("has-error");
$("#email-group").append('<div class="help-block">' + data.errors.email + "</div>");
}
} else { // Success
$("form").html('<script></script>' + '<div class="alert alert-success">' + data.message + "</div>");
window.location.href = '/Registration_and_Login/Welcome_page.php';
}
})
.fail(function (data) {
// $("#message-group").html('<div class="alert alert-danger">Could not reach server, please try again later.</div>');
});
event.preventDefault();
});
});
//Check if the date is valid
function validatedate(dateString) {
let dateformat = /^(0?[1-9]|1[0-2])[\/](0?[1-9]|[1-2][0-9]|3[01])[\/]\d{4}$/;
// Match the date format through regular expression
if (dateString.match(dateformat)) {
let operator = dateString.split('/');
// Extract the string into month, date and year
let datepart = [];
if (operator.length > 1) {
pdatepart = dateString.split('/');
}
let month = parseInt(datepart[0]);
let day = parseInt(datepart[1]);
let year = parseInt(datepart[2]);
// Create list of days of a month
let ListofDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (month == 1 || month > 2) {
if (day > ListofDays[month - 1]) {
///This check is for Confirming that the date is not out of its range
return false;
}
} else if (month == 2) {
let leapYear = false;
if ((!(year % 4) && year % 100) || !(year % 400)) {
leapYear = true;
}
if ((leapYear == false) && (day >= 29)) {
return false;
} else if ((leapYear == true) && (day > 29)) {
alert('Invalid date format!');
return false;
}
}
} else {
alert("Invalid date format!");
return false;
}
return true;
}