674754aaadf0d2496d0480b2655e08d69f4e9f92
1 /***************************************************************
3 * Universal validate-form
9 * (c) 1998-2008 Kasper Skaarhoj
12 * This script is part of the TYPO3 t3lib/ library provided by
13 * Kasper Skaarhoj <kasper@typo3.com> together with TYPO3
15 * Released under GNU/GPL (see license file in tslib/)
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.
21 * This copyright notice MUST APPEAR in all copies of this script
22 ***************************************************************/
25 function validateForm(theFormname
,theFieldlist
,goodMess
,badMess
,emailMess
) {
26 var formObject
= document
[theFormname
];
28 formObject
= document
.getElementById(theFormname
);
30 if (formObject
&& theFieldlist
) {
32 var theField
= split(theFieldlist
, ",", index
);
42 // Check special modes:
43 if (theField
== '_EREG') { // EREG mode: _EREG,[error msg],[JS ereg],[fieldname],[field Label]
44 specialMode
= theField
;
47 theEregMsg
= unescape(split(theFieldlist
, ",", index
));
49 theEreg
= unescape(split(theFieldlist
, ",", index
));
50 } else if (theField
== '_EMAIL') {
51 specialMode
= theField
;
54 // Get real field name if special mode has been set:
57 theField
= split(theFieldlist
, ",", index
);
61 theLabel
= unescape(split(theFieldlist
, ",", index
));
62 theField
= unescape(theField
);
63 if (formObject
[theField
]) {
64 var fObj
= formObject
[theField
];
77 if (fObj
.selectedIndex
>=0) {
78 value
= fObj
.options
[fObj
.selectedIndex
].value
;
81 case "select-multiple":
84 if (fObj
.options
[a
].selected
) {
85 value
+= fObj
.options
[a
].value
;
94 if (fObj
[a
].checked
) {
95 value
= fObj
[a
].value
;
108 switch(specialMode
) {
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!')+')';
117 var theRegEx_isValid
= new RegExp(theEreg
,"");
118 if (!theRegEx_isValid
.test(value
)) {
119 msg
+="\n"+theLabel
+' ('+theEregMsg
+')';
129 theField
= split(theFieldlist
, ",", index
);
132 var theBadMess
= unescape(badMess
);
134 theBadMess
= "You must fill in these fields:";
137 alert(theBadMess
+msg
);
140 var theGoodMess
= unescape(goodMess
);
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;}
157 ePos
= theStr
.indexOf(delim
, sPos
+lengthOfDelim
);
158 if(ePos
== -1) {ePos
= theStr
.length
;}
159 return (theStr
.substring(sPos
+lengthOfDelim
,ePos
));