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