form.js
1.92 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
/**
* @package Regular Labs Library
* @version 18.2.10140
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
var RegularLabsForm = null;
(function($) {
"use strict";
RegularLabsForm = {
getValue: function(name, escape) {
var $field = $('[name="' + name + '"]');
if (!$field.length) {
$field = $('[name="' + name + '[]"]');
}
if (!$field.length) {
return;
}
var type = $field.attr('type');
if (typeof type == "undefined" && $field.prop("tagName").toLowerCase() == 'select') {
type = 'select';
}
switch (type) {
case 'checkbox':
return this.getValuesFromList($('[name="' + name + '[]"]:checked'), escape);
case 'select':
return this.getValuesFromList($field.find('option:checked'), escape);
case 'radio':
$field = $('[name="' + name + '"]:checked');
break;
}
return this.prepareValue($field.val(), escape);
},
getValuesFromList: function($elements, escape) {
var self = this;
var values = [];
$elements.each(function() {
values.push(self.prepareValue($(this).val(), escape));
});
return values;
},
prepareValue: function(value, escape) {
if (!isNaN(value) && value.indexOf('.') < 0) {
return parseInt(value);
}
if (escape) {
value = value.replace(/"/g, '\\"');
}
return value.trim();
},
toTextValue: function(str) {
return (str + '').replace(/^[\s-]*/, '').trim();
},
toSimpleValue: function(str) {
return (str + '').toLowerCase().replace(/[^0-9a-z]/g, '').trim();
},
preg_quote: function(str) {
return (str + '').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '\\$1');
},
escape: function(str) {
return (str + '').replace(/([\"])/g, '\\$1');
}
}
})(jQuery);