home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2003 November
/
Chip_2003-11_cd1.bin
/
chplus
/
chlapark
/
check_form.js
< prev
next >
Wrap
Text File
|
2003-09-29
|
8KB
|
244 lines
/*
checkForm by Riki "Fczbkk" Fridrich, 2002
ver 2.0
http://www.fczbkk.sk/
mailto:acid@pobox.sk
You should find latest version of this script and documentation at
http://www.fczbkk.sk/js/check_form/
*/
/*
This is second version of my script for automatic checking of forms. It should
work in in all standards-compliant browsers (Mozilla, Internet Explorer 5+), it
downgrades OK in older browsers. No, it's not working in Opera (I said it works
in standards-compliant browsers only).
It requires additional functions and libraries:
- event_attacher.js
http://www.fczbkk.sk/js/event_attacher/
- fix_e.js
http://www.fczbkk.sk/js/fix_e/
- class_magic.js
http://www.fczbkk.sk/js/class_magic/
ATTENTION!!! Additional libraries and functions must be placed/linked before
checkForm.
All used scripts and libraries are writen by me, except "Event Attacher", which
was writen by Scott Andrew (http://www.scottandrew.com/). Feel free to use
and/or modify them (as far as I know, "Event Attacher" is free to use too), just
please let me know where you used it if possible (I want to see it in action).
Any suggestions, comments, bugreports, modifications or functionality
enhancements are wellcome.
*/
/*
Formatting of the fields (e.g. highlighting of "required" or "invalid" fields)
is defined in external CSS file "check_field.css". These definitions can be
modified to fit your layout and they can be moved anywhere into your CSS
definitions.
*/
// object holding all other functions and variables
var checkForm = {};
/*
Regular expression builder, used for simplier adding or editing of character
groups.
*/
checkForm.buildRegExp = function(str) {
if (str) {return new RegExp("^[" + str + "]{1,}$");}
return false;
}
/* -------------- customizations start here -------------- */
/*
Message, which will be displayed in the alert, when user tries to submit
incomplete or invalid form.
*/
checkForm.invalidMsg = "Nektere polozky odesilaneho formulare nejsou vyplneny spravne.\n\nProsim, vyplnte vsechy povinne polozky (oznacene cervenou teckou) a opravte polozky s nespravnym obsahem (oznacene cervenym textem) a zkuste odeslat formular znova.";
/*
Acceptable characters or regular expressions defining valid content of checked
classes.
*/
checkForm.fieldType = new Array();
checkForm.fieldType["safeChars"] = checkForm.buildRegExp("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_");
checkForm.fieldType["numbers"] = checkForm.buildRegExp("0123456789");
checkForm.fieldType["alphabetLowercase"] = checkForm.buildRegExp("abcdefghijklmnopqrstuvwxyz");
checkForm.fieldType["alphabetUppercase"] = checkForm.buildRegExp("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
checkForm.fieldType["alphabet"] = checkForm.buildRegExp("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
checkForm.fieldType["alphanumeric"] = checkForm.buildRegExp("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
checkForm.fieldType["date"] = new RegExp("^[0-9]{1,2}[.][0-9]{1,2}[.][0-9]{4}$"); // in format (D)D.(M)M.YYYY
/* IE4 nezvlada tento retulerny vyraz (URL) */
checkForm.fieldType["url"] = new RegExp("^http:\/\/[a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])*\.[a-zA-Z]{2,4}(\/{1}[-_~&=\?\.a-z0-9]*)*$");
checkForm.fieldType["email"] = new RegExp("^[a-z0-9]+[a-z0-9\._-]*[a-z0-9]+@[a-z0-9]+[a-z0-9\._-]*[a-z0-9]+\.[a-z]{2,4}$");
/*
Default values for classes. These will be displayed, if form field with given
class is focused and empty.
*/
checkForm.defaultValue = new Array();
checkForm.defaultValue["url"] = "http://";
/* -------------- customizations end here -------------- */
/*
Initialisation of checkForm when document is loaded. It finds all forms and
fields, checks them and adds events to them.
*/
checkForm.init = function() {
var forms = document.getElementsByTagName("form");
for (var i = 0; i < forms.length; i++) {
addEvent(forms[i], "submit", checkForm.check);
addEvent(forms[i], "reset", checkForm.check);
}
var fields = checkForm.findInputs();
for (var i = 0; i < fields.length; i++) {
checkForm.checkField(fields[i]);
addEvent(fields[i], "blur", checkForm.checkField);
addEvent(fields[i], "focus", checkForm.checkField);
addEvent(fields[i], "keyup", checkForm.checkField);
addEvent(fields[i], "change", checkForm.checkField);
}
}
/*
Helper functions, which finds all fields in given element and returns them in
an array. If no element is specified, all fields in document are returned.
*/
checkForm.findInputs = function(elm) {
var fields = new Array();
if (!elm) {elm = document;}
var textareas = elm.getElementsByTagName("textarea");
var inputs = elm.getElementsByTagName("input");
for (var i = 0; i < textareas.length; i++) {
fields[fields.length] = textareas[i];
}
for (var i = 0; i < inputs.length; i++) {
if ((inputs[i].type == "text") || (inputs[i].type == "password")) {
fields[fields.length] = inputs[i];
}
}
return fields;
}
/*
Checking the form before submitting. To be sure, we will check all fields of
given form once more and cancel the submit action if some fields are invalid.
*/
checkForm.check = function(e) {
e = fixE(e);
var obj = e.target;
while (obj.tagName != "FORM") {
obj = obj.parentNode;
}
var fields = checkForm.findInputs(obj);
for (var i = 0; i < fields.length; i++) {
if (checkForm.checkField(fields[i]) == "invalid") {
if (e.type == "submit") {
alert(checkForm.invalidMsg);
if (e.preventDefault) {
e.preventDefault();
}
}
return false;
}
}
return true;
}
/*
Checking the form field.
*/
checkForm.checkField = function(e) {
var obj;
if (e && e.tagName) {
obj = e;
} else {
e = fixE(e);
obj = e.target;
}
var fieldOK = true;
var classes = classMagic.get(obj);
// required field can't be empty
if (classMagic.has(obj, "required")) {
fieldOK = (checkForm.isBlank(obj.value)) ? false : true;
}
// find classes of given field and check, if there are any rules for it
var i = 0;
while (fieldOK && i < classes.length) {
if (checkForm.defaultValue[classes[i]]) {
if ((e.type == "focus") && checkForm.isBlank(obj.value)) {
obj.value = checkForm.defaultValue[classes[i]];
}
if ((e.type == "blur") && (obj.value == checkForm.defaultValue[classes[i]])) {
obj.value = "";
}
}
if (checkForm.fieldType[classes[i]]) {
fieldOK = (!checkForm.isBlank(obj.value) && checkForm.checkChars(obj.value, checkForm.fieldType[classes[i]])) ? false : true;
}
i++;
}
// if content of the field is not OK, highlight it as invalid
(fieldOK) ? classMagic.remove(obj, "invalid") : classMagic.add(obj, "invalid");
/*
Function like this should return "true" or "false". But this would be
blocking of keyboard input in case of incomplete content of the field
(e.g. URL). That's why this function returns "valid" or "invalid".
*/
return (fieldOK) ? "valid" : "invalid";
}
/*
Checks if given string contains given pattern (regullar expression).
*/
checkForm.checkChars = function(str, re) {
if (str && re) {
return (str.search(re) == -1) ? true : false;
}
return true;
}
/*
Checks, if given string is blank or not.
*/
checkForm.isBlank = function(str) {
return (str == "") ? true : false;
}
/*
Initialize script when document is loaded.
*/
addEvent(window, "load", checkForm.init);