[ Index ] |
|
Code source de Typo3 4.1.3 |
1 /*************************************************************** 2 * 3 * Universal validate-form 4 * 5 * $Id: jsfunc.validateform.js 2312 2007-05-01 17:15:10Z masi $ 6 * 7 * Copyright notice 8 * 9 * (c) 1998-2007 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 var len=fObj.length; 91 if (len) { 92 for (a=0;a<len;a++) { 93 if (fObj[a].checked) { 94 value = fObj[a].value; 95 } 96 } 97 } else { 98 if (fObj.checked) { 99 value = fObj.value; 100 } 101 } 102 break; 103 default: 104 value = 1; 105 } 106 107 switch(specialMode) { 108 case "_EMAIL": 109 var theRegEx_notValid = new RegExp("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", "gi"); 110 var theRegEx_isValid = new RegExp("^.+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})$",""); 111 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? 112 msg+="\n"+theLabel+' ('+(emailMess ? unescape(emailMess) : 'Email address not valid!')+')'; 113 } 114 break; 115 case "_EREG": 116 var theRegEx_isValid = new RegExp(theEreg,""); 117 if (!theRegEx_isValid.test(value)) { 118 msg+="\n"+theLabel+' ('+theEregMsg+')'; 119 } 120 break; 121 default: 122 if (!value) { 123 msg+="\n"+theLabel; 124 } 125 } 126 } 127 index++; 128 theField = split(theFieldlist, ",", index); 129 } 130 if (msg) { 131 var theBadMess = unescape(badMess); 132 if (!theBadMess) { 133 theBadMess = "You must fill in these fields:"; 134 } 135 theBadMess+="\n"; 136 alert(theBadMess+msg); 137 return false; 138 } else { 139 var theGoodMess = unescape(goodMess); 140 if (theGoodMess) { 141 alert(theGoodMess); 142 } 143 return true; 144 } 145 } 146 } 147 function split(theStr1, delim, index) { 148 var theStr = ''+theStr1; 149 var lengthOfDelim = delim.length; 150 sPos = -lengthOfDelim; 151 if (index<1) {index=1;} 152 for (a=1; a<index; a++) { 153 sPos = theStr.indexOf(delim, sPos+lengthOfDelim); 154 if (sPos==-1) {return null;} 155 } 156 ePos = theStr.indexOf(delim, sPos+lengthOfDelim); 157 if(ePos == -1) {ePos = theStr.length;} 158 return (theStr.substring(sPos+lengthOfDelim,ePos)); 159 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Nov 25 17:13:16 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |