-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
77 lines (73 loc) · 2.5 KB
/
class.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
function verify(type,input) {
var regexes = {'email':/^([\S]+)@([\S]+)\.([\S]+)$/,'phone':/^[0-9]{10}$/,'code':/^([a-zA-Z]{3})\-([0-9]{3})$/,'roll':/^([0-9]{3})\/([a-zA-z]{2})\/([0-9]{2})$/,'name':/^[a-zA-Z \']+$/,'num':/^[0-9]+$/};
return ((input.match(regexes[type]) == null)? false:true);
}
$(document).ready(function() {
updateClass();
$('input[name=code],input[name=year],input[name=semester],input[name=section]').on('keyup',function() {
$(this).parent().find('.update').html('Update');
});
});
function updateClass() {
$('.update').click(function() {
var btn = $(this);
var details = btn.parent();
var d = {code:'',year:'',section:'',semester:'',class_id:''};
d.code = details.find("input[name=code]").val();
d.class_id = details.attr('id').replace(/_/g,'');
d.year = details.find("input[name=year]").val();
d.semester = details.find("input[name=semester]").val();
d.section = details.find("input[name=section]").val();
console.log(d);
if(!verify('code',d.code)) {
alert('Invalid code');
return;
}
if((!verify('number',d.year)) || d.year > (new Date()).getFullYear() || d.year < 1983 ) {
alert('Invalid year');
return;
}
if((!verify('number',d.section)) || d.section < 1 || d.section > 3) {
alert('Invalid section');
return;
}
if((!verify('number',d.semester)) || d.semester < 1 || d.semester > 9) {
alert('Invalid semester');
return;
}
$.ajax({
url : 'php/update_class.php',
data : d,
type : 'post',
dataType : 'json',
success : function(r) {
console.log(r);
switch(r.error) {
case 'none' :
btn.html('<strong>Updated!</strong>');
break;
case 'failure' :
alert("We are facing some issues. Logging you out for safety");
window.location = "logout.php";
break;
case 'not_found' :
alert("We are facing some issues. Logging you out for safety");
window.location = "logout.php";
break;
case 'year' :
alert("Invalid Year!");
break;
case 'semester' :
alert("Invalid Semester!");
break;
case 'section' :
alert("Invalid Section!");
break;
case 'code' :
alert("Invalid Code!");
break;
}
}
});
});
}