Gazelle/static/functions/validate.js

127 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-04-30 18:18:07 +00:00
var elemStyles = Array();
var errorElems = Array();
2011-03-28 14:21:28 +00:00
function validEmail(str) {
2013-02-22 08:00:24 +00:00
if (str.match(/^[_a-z0-9-]+([.+][_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i)) {
2011-03-28 14:21:28 +00:00
return true;
2013-02-22 08:00:24 +00:00
} else {
2011-03-28 14:21:28 +00:00
return false;
}
}
function validLink(str) {
if (str.match(/^(https?):\/\/([a-z0-9\-\_]+\.)+([a-z]{1,5}[^\.])(\/[^<>]+)*$/i)) {
return true;
2013-04-30 18:18:07 +00:00
} else {
return false;
}
2011-03-28 14:21:28 +00:00
}
function isNumeric(str,usePeriod) {
2013-04-30 18:18:07 +00:00
matchStr = '/[^0-9';
2011-03-28 14:21:28 +00:00
if (usePeriod) {
2013-04-30 18:18:07 +00:00
matchStr += '\.';
2011-03-28 14:21:28 +00:00
}
2013-04-30 18:18:07 +00:00
matchStr = ']/';
2011-03-28 14:21:28 +00:00
2013-04-30 18:18:07 +00:00
if (str.match(matchStr)) {
return false;
}
2011-03-28 14:21:28 +00:00
return true;
}
function validDate(theDate) {
2013-04-30 18:18:07 +00:00
days = 0;
2011-03-28 14:21:28 +00:00
2013-04-30 18:18:07 +00:00
theDate = theDate.split("/");
month = theDate[0];
day = theDate[1];
year = theDate[2];
2011-03-28 14:21:28 +00:00
if (!isNumeric(month) || !isNumeric(day) || !isNumeric(year)) { return false; }
2013-04-30 18:18:07 +00:00
if (month == 1) { days = 31; }
else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}}
else if (month == 3) { days = 31; }
else if (month == 4) { days = 30; }
else if (month == 5) { days = 31; }
else if (month == 6) { days = 30; }
else if (month == 7) { days = 31; }
else if (month == 8) { days = 31; }
else if (month == 9) { days = 30; }
else if (month == 10) { days = 31; }
else if (month == 11) { days = 30; }
else if (month == 12) { days = 31; }
if (day > days || day == undefined || days == undefined || month == undefined || year == undefined || year.length < 4) {
return false;
} else {
return true;
}
2011-03-28 14:21:28 +00:00
}
function showError(fields,alertStr) {
var tField=Array();
2013-06-06 08:01:03 +00:00
var obj, el;
2011-03-28 14:21:28 +00:00
2013-04-30 18:18:07 +00:00
if (typeof(fields) == 'object') {
tField[0] = fields;
} else {
tField = fields.split(',');
}
for (s = 0; s <= tField.length - 1; s++) {
2013-06-06 08:01:03 +00:00
obj = $('#'+tField[s]);
if (obj) {
el = obj.raw();
obj.add_class("elem_error");
2013-04-30 18:18:07 +00:00
if (s == 0) {
2013-06-06 08:01:03 +00:00
el.focus();
2013-04-30 18:18:07 +00:00
try {
2013-06-06 08:01:03 +00:00
el.select();
2013-04-30 18:18:07 +00:00
} catch (error) {
}
2011-03-28 14:21:28 +00:00
}
2013-06-06 08:01:03 +00:00
var evtType = el.type == "select-one" ? "change" : "keypress";
obj.listen(evtType, clearElemError);
errorElems.push(tField[s]);
2011-03-28 14:21:28 +00:00
}
}
2013-04-30 18:18:07 +00:00
if (alertStr != "") {
alert(alertStr);
}
2011-03-28 14:21:28 +00:00
return false;
}
function clearErrors(theForm) {
2013-04-30 18:18:07 +00:00
elementList = document.forms[theForm].elements;
for (x = 0; x <= elementList.length - 1; x++) {
if (elementList[x].type != "submit" && elementList[x].type != "button") {
2011-03-28 14:21:28 +00:00
if (!elemStyles[elementList[x].id]) {
2013-04-30 18:18:07 +00:00
elemStyles[elementList[x].id] = elementList[x].className;
2011-03-28 14:21:28 +00:00
}
try {
2013-04-30 18:18:07 +00:00
elementList[x].className = elemStyles[elementList[x].id];
2011-03-28 14:21:28 +00:00
} catch (error) { }
}
}
}
function clearElemError(evt) {
2013-06-06 08:01:03 +00:00
var obj, el;
2013-04-30 18:18:07 +00:00
for (x = 0; x <= errorElems.length - 1; x++) {
2013-06-06 08:01:03 +00:00
obj = $('#'+errorElems[x]);
el = obj.raw();
var evtType = el.type == "select-one" ? "change" : "keypress";
obj.unbind(evtType, clearElemError);
el.className = elemStyles[el.id];
2011-03-28 14:21:28 +00:00
}
2013-04-30 18:18:07 +00:00
errorElems = Array();
2011-03-28 14:21:28 +00:00
}