-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformhandler.jquery.js
More file actions
182 lines (173 loc) · 5.16 KB
/
formhandler.jquery.js
File metadata and controls
182 lines (173 loc) · 5.16 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
;(function($) {
"use strict";
var placeholders = [],
elements = [],
form = null,
options = {},
isSending = false;
var methods = {
init: function(_options, callback) {
form = this;
options = $.extend({
'ajax': false,
'customSubmit': false,
'enableSubmitOnSuccess': false,
'disableSubmitOnSend': true,
'placeholder': true,
'validate': true
}, _options);
$(this).find('input, textarea').each(function(i, obj){
elements[i] = obj;
});
for (var i in elements) {
if (elements[i].type === 'submit')
continue;
if (elements[i].type !== 'checkbox' &&
elements[i].type !== 'radio') {
placeholders[i] = elements[i].value;
$(elements[i]).attr('id', i);
$(elements[i]).focus(function(e){
_methods.focus(e, this);
});
$(elements[i]).blur(function(e){
_methods.blur(e, this);
});
}
}
if (options.customSubmit) {
options.customSubmit.click(function() {
$(form).submit();
});
}
$(this).submit(function(e) {
e.preventDefault();
if (options.isSending)
return;
if ((options.validate && _methods.validate(this)) ||
!options.validate) {
if (options.disableSubmitOnSend)
_methods.disable();
options.isSending = true;
var result = _methods.jsonisize();
if (callback)
callback(result);
if (options.ajax)
_methods.send(result);
}
});
}
};
var _methods = {
focus: function(e, obj) {
if (obj.value === placeholders[$(obj).attr('id')])
$(obj).val('');
},
blur: function(e, obj) {
if (obj.value === '')
$(obj).val(placeholders[$(obj).attr('id')]);
},
validate: function(form) {
var err = false;
for (var i in elements) {
var element = elements[i];
if (element.type === 'checkbox' ||
element.type === 'submit')
continue;
if (element.type === 'radio' && $(element).attr('required')) {
var radios = document.getElementsByName(element.name);
var checked = false;
for (var j in radios) {
if (radios[j].checked)
checked = true;
}
if (!checked) {
element.focus();
return false;
}
}
if (element.value === placeholders[$(element).attr('id')] &&
$(element).attr('required')) {
element.focus();
return false;
}
if ($(element).attr('validate')) {
var method = $(element).attr('validate');
method = method.charAt(0).toUpperCase() + method.substr(1, method.length);
if (typeof _methods['is' + method] != 'undefined') {
var obj = _methods['is' + method](element.value);
if (!obj) {
element.focus();
return false;
} else {
if (typeof obj === 'string')
element.value = obj;
}
}
}
}
return true;
},
isPhone: function(obj) {
obj = obj.replace(/[^0-9+]*/g, '');
var regex = /(07|\+467)([0-9]{8,8}$)/g;
return regex.test(obj) ? obj : false;
},
isEmail: function(obj) {
var regex = /^[a-zA-Z\-_.]+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
return regex.test(obj);
},
jsonisize: function() {
var jsonDict = {};
for (var i in elements) {
if (elements[i].type === 'checkbox') {
if (elements[i].checked)
jsonDict[elements[i].name] = 'Yes';
else
jsonDict[elements[i].name] = 'No';
} else if (elements[i].type === 'radio') {
if (elements[i].checked)
jsonDict[elements[i].name] = elements[i].value;
} else
if (elements[i].type !== 'submit')
jsonDict[elements[i].name] = elements[i].value;
}
return jsonDict;
},
disable: function() {
if (options.customSubmit)
$(options.customSubmit).addClass('disabled');
$(form).find(':submit').attr('disabled', 'disabled');
},
enable: function() {
if (options.customSubmit)
$(options.customSubmit).removeClass('disabled');
$(form).find(':submit').removeAttr('disabled');
},
send: function(data) {
$.ajax({
url: options.ajax.action,
type: 'post',
dataType: 'json',
data: {
action: options.ajax.method || 'form',
data: data
},
success: function(r) {
options.isSending = false;
if (options.enableSubmitOnSuccess)
_methods.enable();
if (options.ajax.success)
options.ajax.success(r);
}
});
}
};
$.fn.FormHandler = function(method, callback) {
if (typeof method === 'object') {
return methods.init.apply(this, arguments, callback);
} else if (typeof method === 'undefined')
return methods.init.apply(this);
else
$.error('Unable to call method "' + method + '"');
};
})(jQuery);