1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * function used in table data manipulation pages
9 * Modify from controls when the "NULL" checkbox is selected
11 * @param string the MySQL field type
12 * @param string the urlencoded field name - OBSOLETE
13 * @param string the md5 hashed field name
14 * @param string the multi_edit row sequence number
16 * @return boolean always true
18 function nullify(theType
, urlField
, md5Field
, multi_edit
)
20 var rowForm
= document
.forms
['insertForm'];
22 if (typeof(rowForm
.elements
['funcs' + multi_edit
+ '[' + md5Field
+ ']']) != 'undefined') {
23 rowForm
.elements
['funcs' + multi_edit
+ '[' + md5Field
+ ']'].selectedIndex
= -1;
26 // "SET" field , "ENUM" field with more than 20 characters
27 // or foreign key field (drop-down)
28 if (theType
== 1 || theType
== 3 || theType
== 4) {
29 rowForm
.elements
['field_' + md5Field
+ multi_edit
+ '[]'].selectedIndex
= -1;
32 else if (theType
== 2) {
33 var elts
= rowForm
.elements
['field_' + md5Field
+ multi_edit
+ '[]'];
34 // when there is just one option in ENUM:
38 var elts_cnt
= elts
.length
;
39 for (var i
= 0; i
< elts_cnt
; i
++ ) {
40 elts
[i
].checked
= false;
45 // foreign key field (with browsing icon for foreign values)
46 else if (theType
== 6) {
47 rowForm
.elements
['field_' + md5Field
+ multi_edit
+ '[]'].value
= '';
50 else /*if (theType == 5)*/ {
51 rowForm
.elements
['fields' + multi_edit
+ '[' + md5Field
+ ']'].value
= '';
52 } // end if... else if... else
55 } // end of the 'nullify()' function
59 * javascript DateTime format validation.
60 * its used to prevent adding default (0000-00-00 00:00:00) to database when user enter wrong values
61 * Start of validation part
63 //function checks the number of days in febuary
64 function daysInFebruary (year
){
65 return (((year
% 4 == 0) && ( (!(year
% 100 == 0)) || (year
% 400 == 0))) ? 29 : 28 );
67 //function to convert single digit to double digit
68 function fractionReplace(num
)
74 case 1:res
= "01";break;
75 case 2:res
= "02";break;
76 case 3:res
= "03";break;
77 case 4:res
= "04";break;
78 case 5:res
= "05";break;
79 case 6:res
= "06";break;
80 case 7:res
= "07";break;
81 case 8:res
= "08";break;
82 case 9:res
= "09";break;
87 /* function to check the validity of date
88 * The following patterns are accepted in this validation (accepted in mysql as well)
92 * 4) And instead of using '-' the following punctuations can be used (+,.,*,^,@,/) All these are accepted by mysql as well. Therefore no issues
94 function isDate(val
,tmstmp
)
96 val
=val
.replace(/[.|*|^|+|//|@]/g,'-');
97 var arrayVal
=val
.split("-");
98 for(var a
=0;a
<arrayVal
.length
;a
++)
100 if(arrayVal
[a
].length
==1)
101 arrayVal
[a
]=fractionReplace(arrayVal
[a
]);
103 val
=arrayVal
.join("-");
105 dtexp
=new RegExp(/^([0-9]{4})-(((01|03|05|07|08|10|12)-((0[0-9])|([1-2][0-9])|(3[0-1])))|((02|04|06|09|11)-((0[0-9])|([1-2][0-9])|30)))$/);
108 dtexp
=new RegExp(/^([0-9]{2})-(((01|03|05|07|08|10|12)-((0[0-9])|([1-2][0-9])|(3[0-1])))|((02|04|06|09|11)-((0[0-9])|([1-2][0-9])|30)))$/);
113 var month
=parseInt(val
.substring(pos
+3,pos
+5));
114 var day
=parseInt(val
.substring(pos
+6,pos
+8));
115 var year
=parseInt(val
.substring(0,pos
+2));
116 if(month
==2&&day
>daysInFebruary(year
))
118 if(val
.substring(0,pos
+2).length
==2)
120 if(val
.substring(0,pos
+2).length
==2)
121 year
=parseInt("20"+val
.substring(0,pos
+2));
123 year
=parseInt("19"+val
.substring(0,pos
+2));
127 if(year
<1978) return false;
128 if(year
>2038||(year
>2037&&day
>19&&month
>=1)||(year
>2037&&month
>1)) return false;
136 /* function to check the validity of time
137 * The following patterns are accepted in this validation (accepted in mysql as well)
143 var arrayVal
=val
.split(":");
144 for(var a
=0;a
<arrayVal
.length
;a
++)
146 if(arrayVal
[a
].length
==1)
147 arrayVal
[a
]=fractionReplace(arrayVal
[a
]);
149 val
=arrayVal
.join(":");
150 tmexp
=new RegExp(/^(([0-1][0-9])|(2[0-3])):((0[0-9])|([1-5][0-9])):((0[0-9])|([1-5][0-9]))$/);
155 //validate the datetime and integer
156 function Validator(urlField
, multi_edit
,theType
){
157 var rowForm
= document
.forms
['insertForm'];
158 var evt
= window
.event
|| arguments
.callee
.caller
.arguments
[0];
159 var target
= evt
.target
|| evt
.srcElement
;
160 unNullify(urlField
, multi_edit
);
162 if(target
.name
.substring(0,6)=="fields")
164 var dt
=rowForm
.elements
['fields[multi_edit][' + multi_edit
+ '][' + urlField
+ ']'];
165 // validate for date time
166 if(theType
=="datetime"||theType
=="time"||theType
=="date"||theType
=="timestamp")
169 if(!isDate(dt
.value
))
171 dt
.className
="invalid_value";
175 else if(theType
=="time")
177 if(!isTime(dt
.value
))
179 dt
.className
="invalid_value";
183 else if(theType
=="datetime"||theType
=="timestamp")
186 if(dt
.value
=="CURRENT_TIMESTAMP")
191 if(theType
=="timestamp")
195 if(dt
.value
=="0000-00-00 00:00:00")
197 var dv
=dt
.value
.indexOf(" ");
200 dt
.className
="invalid_value";
205 if(!(isDate(dt
.value
.substring(0,dv
),tmstmp
)&&isTime(dt
.value
.substring(dv
+1))))
207 dt
.className
="invalid_value";
213 //validate for integer type
214 if(theType
.substring(0,3)=="int"){
217 dt
.className
="invalid_value";
225 /* End of datetime validation*/
228 * Unchecks the "NULL" control when a function has been selected or a value
231 * @param string the urlencoded field name
232 * @param string the multi_edit row sequence number
234 * @return boolean always true
236 function unNullify(urlField
, multi_edit
)
238 var rowForm
= document
.forms
['insertForm'];
240 if (typeof(rowForm
.elements
['fields_null[multi_edit][' + multi_edit
+ '][' + urlField
+ ']']) != 'undefined') {
241 rowForm
.elements
['fields_null[multi_edit][' + multi_edit
+ '][' + urlField
+ ']'].checked
= false
244 if (typeof(rowForm
.elements
['insert_ignore_' + multi_edit
]) != 'undefined') {
245 rowForm
.elements
['insert_ignore_' + multi_edit
].checked
= false
249 } // end of the 'unNullify()' function