ccdc87583fda6a6da88164f69593f13c17d509d8
[Packages/TYPO3.CMS.git] / t3lib / jsfunc.validateform.js
1 /***************************************************************
2 *
3 * Universal validate-form
4 *
5 * $Id$
6 *
7 *
8 *
9 * Copyright notice
10 *
11 * (c) 1998-2003 Kasper Skaarhoj
12 * All rights reserved
13 *
14 * This script is part of the TYPO3 t3lib/ library provided by
15 * Kasper Skaarhoj <kasper@typo3.com> together with TYPO3
16 *
17 * Released under GNU/GPL (see license file in tslib/)
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * This copyright notice MUST APPEAR in all copies of this script
24 ***************************************************************/
25
26
27 function validateForm(theFormname,theFieldlist,goodMess,badMess,emailMess) {
28 var formObject = document[theFormname];
29 if (!formObject) {
30 formObject = document.getElementById(theFormname);
31 }
32 if (formObject && theFieldlist) {
33 var index=1;
34 var theField = split(theFieldlist, ",", index);
35 var msg="";
36 var theEreg = '';
37 var theEregMsg = '';
38 var specialMode = '';
39
40 while (theField) {
41 theEreg = '';
42 specialMode = '';
43
44 // Check special modes:
45 if (theField == '_EREG') { // EREG mode: _EREG,[error msg],[JS ereg],[fieldname],[field Label]
46 specialMode = theField;
47
48 index++;
49 theEregMsg = unescape(split(theFieldlist, ",", index));
50 index++;
51 theEreg = unescape(split(theFieldlist, ",", index));
52 } else if (theField == '_EMAIL') {
53 specialMode = theField;
54 }
55
56 // Get real field name if special mode has been set:
57 if (specialMode) {
58 index++;
59 theField = split(theFieldlist, ",", index);
60 }
61
62 index++;
63 theLabel = unescape(split(theFieldlist, ",", index));
64 theField = unescape(theField);
65 if (formObject[theField]) {
66 var fObj = formObject[theField];
67 var type=fObj.type;
68 if (!fObj.type) {
69 type="radio";
70 }
71 var value="";
72 switch(type) {
73 case "text":
74 case "textarea":
75 value = fObj.value;
76 break;
77 case "select-one":
78 if (fObj.selectedIndex>=0) {
79 value = fObj.options[fObj.selectedIndex].value;
80 }
81 break;
82 case "select-multiple":
83 var l=fObj.length;
84 for (a=0;a<l;a++) {
85 if (fObj.options[a].selected) {
86 value+= fObj.options[a].value;
87 }
88 }
89 break;
90 case "radio":
91 var l=fObj.length;
92 for (a=0; a<l;a++) {
93 if (fObj[a].checked) {
94 value = fObj[a].value;
95 }
96 }
97 break;
98 default:
99 value=1;
100 }
101
102 switch(specialMode) {
103 case "_EMAIL":
104 var theRegEx_notValid = new RegExp("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", "gi");
105 var theRegEx_isValid = new RegExp("^.+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})$","");
106 if (!theRegEx_isValid.test(value)) { // This part was supposed to be a part of the condition: " || theRegEx_notValid.test(value)" - but I couldn't make it work (Mozilla Firefox, linux) - Anyone knows why?
107 msg+="\n"+theLabel+' ('+(emailMess ? unescape(emailMess) : 'Email address not valid!')+')';
108 }
109 break;
110 case "_EREG":
111 var theRegEx_isValid = new RegExp(theEreg,"");
112 if (!theRegEx_isValid.test(value)) {
113 msg+="\n"+theLabel+' ('+theEregMsg+')';
114 }
115 break;
116 default:
117 if (!value) {
118 msg+="\n"+theLabel;
119 }
120 }
121 }
122 index++;
123 theField = split(theFieldlist, ",", index);
124 }
125 if (msg) {
126 var theBadMess = unescape(badMess);
127 if (!theBadMess) {
128 theBadMess = "You must fill in these fields:";
129 }
130 theBadMess+="\n";
131 alert(theBadMess+msg);
132 return false;
133 } else {
134 var theGoodMess = unescape(goodMess);
135 if (theGoodMess) {
136 alert(theGoodMess);
137 }
138 return true;
139 }
140 }
141 }
142 function split(theStr1, delim, index) {
143 var theStr = ''+theStr1;
144 var lengthOfDelim = delim.length;
145 sPos = -lengthOfDelim;
146 if (index<1) {index=1;}
147 for (a=1; a<index; a++) {
148 sPos = theStr.indexOf(delim, sPos+lengthOfDelim);
149 if (sPos==-1) {return null;}
150 }
151 ePos = theStr.indexOf(delim, sPos+lengthOfDelim);
152 if(ePos == -1) {ePos = theStr.length;}
153 return (theStr.substring(sPos+lengthOfDelim,ePos));
154 }