1/* 2 ------------------------------------------------------------------------- 3 JavaScript Form Validator 4 Version 2.0.2 5 Copyright 2003 JavaScript-coder.com. All rights reserved. 6 You use this script in your Web pages, provided these opening credit 7 lines are kept intact. 8 The Form validation script is distributed free from JavaScript-Coder.com 9 10 You may please add a link to JavaScript-Coder.com, 11 making it easy for others to find this script. 12 Checkout the Give a link and Get a link page: 13 http://www.javascript-coder.com/links/how-to-link.php 14 15 You may not reprint or redistribute this code without permission from 16 JavaScript-Coder.com. 17 18 JavaScript Coder 19 It precisely codes what you imagine! 20 Grab your copy here: 21 http://www.javascript-coder.com/ 22 ------------------------------------------------------------------------- 23*/ 24function Validator(frmname) 25{ 26 this.formobj=document.forms[frmname]; 27 if(!this.formobj) 28 { 29 alert("VnitÅnà chyba - "+frmname+" nenà formuláÅ."); 30 return; 31 } 32 if(this.formobj.onsubmit) 33 { 34 this.formobj.old_onsubmit = this.formobj.onsubmit; 35 this.formobj.onsubmit=null; 36 } 37 else 38 { 39 this.formobj.old_onsubmit = null; 40 } 41 this.formobj.onsubmit=form_submit_handler; 42 this.addValidation = add_validation; 43 this.setAddnlValidationFunction=set_addnl_vfunction; 44 this.clearAllValidations = clear_all_validations; 45} 46function set_addnl_vfunction(functionname) 47{ 48 this.formobj.addnlvalidation = functionname; 49} 50function clear_all_validations() 51{ 52 for(var itr=0;itr < this.formobj.elements.length;itr++) 53 { 54 this.formobj.elements[itr].validationset = null; 55 } 56} 57function form_submit_handler() 58{ 59 for(var itr=0;itr < this.elements.length;itr++) 60 { 61 if(this.elements[itr].validationset && 62 !this.elements[itr].validationset.validate()) 63 { 64 return false; 65 } 66 } 67 if(this.addnlvalidation) 68 { 69 /*str =" var ret = "+this.addnlvalidation+"()"; 70 eval(str); 71 if(!ret) return ret;*/ 72 return this.addnlvalidation(); 73 } 74 return true; 75} 76function add_validation(itemname,descriptor,errstr) 77{ 78 if(!this.formobj) 79 { 80 alert("VniÅnà chyba - objekt validátoru nenà vytvoÅen."); 81 return; 82 }//if 83 var itemobj = this.formobj[itemname]; 84 if(!itemobj) 85 { 86 alert("VnitÅnà chyba - nelze najÃt objekt: "+itemname); 87 return; 88 } 89 if(!itemobj.validationset) 90 { 91 itemobj.validationset = new ValidationSet(itemobj); 92 } 93 itemobj.validationset.add(descriptor,errstr); 94} 95function ValidationDesc(inputitem,desc,error) 96{ 97 this.desc=desc; 98 this.error=error; 99 this.itemobj = inputitem; 100 this.validate=vdesc_validate; 101} 102function vdesc_validate() 103{ 104 if(!V2validateData(this.desc,this.itemobj,this.error)) 105 { 106 this.itemobj.focus(); 107 return false; 108 } 109 return true; 110} 111function ValidationSet(inputitem) 112{ 113 this.vSet=new Array(); 114 this.add= add_validationdesc; 115 this.validate= vset_validate; 116 this.itemobj = inputitem; 117} 118function add_validationdesc(desc,error) 119{ 120 this.vSet[this.vSet.length]= 121 new ValidationDesc(this.itemobj,desc,error); 122} 123function vset_validate() 124{ 125 for(var itr=0;itr<this.vSet.length;itr++) 126 { 127 if(!this.vSet[itr].validate()) 128 { 129 return false; 130 } 131 } 132 return true; 133} 134function validateEmailv2(email) 135{ 136// a very simple email validation checking. 137// you can add more complex email checking if it helps 138 if(email.length <= 0) 139 { 140 return true; 141 } 142 var splitted = email.match("^(.+)@(.+)$"); 143 if(splitted == null) return false; 144 if(splitted[1] != null ) 145 { 146 var regexp_user=/^\"?[\w-_\.]*\"?$/; 147 if(splitted[1].match(regexp_user) == null) return false; 148 } 149 if(splitted[2] != null) 150 { 151 var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/; 152 if(splitted[2].match(regexp_domain) == null) 153 { 154 var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/; 155 if(splitted[2].match(regexp_ip) == null) return false; 156 }// if 157 return true; 158 } 159return false; 160} 161function V2validateData(strValidateStr,objValue,strError) 162{ 163 var epos = strValidateStr.search("="); 164 var command = ""; 165 var cmdvalue = ""; 166 if(epos >= 0) 167 { 168 command = strValidateStr.substring(0,epos); 169 cmdvalue = strValidateStr.substr(epos+1); 170 } 171 else 172 { 173 command = strValidateStr; 174 } 175 switch(command) 176 { 177 case "req": 178 case "required": 179 { 180 if(eval(objValue.value.length) == 0) 181 { 182 if(!strError || strError.length ==0) 183 { 184 strError = objValue.name + ": Povinná položka"; 185 }//if 186 alert(strError); 187 return false; 188 }//if 189 break; 190 }//case required 191 case "maxlength": 192 case "maxlen": 193 { 194 if(eval(objValue.value.length) > eval(cmdvalue)) 195 { 196 if(!strError || strError.length ==0) 197 { 198 strError = objValue.name + ": MaximálnÄ "+cmdvalue+" znaků "; 199 }//if 200 alert(strError + "\n[Nynà = " + objValue.value.length + " ]"); 201 return false; 202 }//if 203 break; 204 }//case maxlen 205 case "minlength": 206 case "minlen": 207 { 208 if(eval(objValue.value.length) < eval(cmdvalue)) 209 { 210 if(!strError || strError.length ==0) 211 { 212 strError = objValue.name + ": MinimálnÄ " + cmdvalue + " znaků "; 213 }//if 214 alert(strError + "\n[Nynà = " + objValue.value.length + " ]"); 215 return false; 216 }//if 217 break; 218 }//case minlen 219 case "alnum": 220 case "alphanumeric": 221 { 222 var charpos = objValue.value.search("[^A-Za-z0-9]"); 223 if(objValue.value.length > 0 && charpos >= 0) 224 { 225 if(!strError || strError.length ==0) 226 { 227 strError = objValue.name+": Povoleny jsou pouze alfanumerické znaky "; 228 }//if 229 alert(strError + "\n [Chybný znak na pozici " + eval(charpos+1)+"]"); 230 return false; 231 }//if 232 break; 233 }//case alphanumeric 234 case "num": 235 case "numeric": 236 { 237 var charpos = objValue.value.search("[^0-9]"); 238 if(objValue.value.length > 0 && charpos >= 0) 239 { 240 if(!strError || strError.length ==0) 241 { 242 strError = objValue.name+": Povolena jsou pouze ÄÃsla "; 243 }//if 244 alert(strError + "\n [Chybný znak na pozici " + eval(charpos+1)+"]"); 245 return false; 246 }//if 247 break; 248 }//numeric 249 case "alphabetic": 250 case "alpha": 251 { 252 var charpos = objValue.value.search("[^A-Za-z]"); 253 if(objValue.value.length > 0 && charpos >= 0) 254 { 255 if(!strError || strError.length ==0) 256 { 257 strError = objValue.name+": Povolena jsou pouze pÃsmena "; 258 }//if 259 alert(strError + "\n [Chybný znak na pozici " + eval(charpos+1)+"]"); 260 return false; 261 }//if 262 break; 263 }//alpha 264 case "alnumhyphen": 265 { 266 var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 267 if(objValue.value.length > 0 && charpos >= 0) 268 { 269 if(!strError || strError.length ==0) 270 { 271 strError = objValue.name+": Povolené znaky jsou A-Z,a-z,0-9,- a _"; 272 }//if 273 alert(strError + "\n [Chybný znak na pozici " + eval(charpos+1)+"]"); 274 return false; 275 }//if 276 break; 277 } 278 case "email": 279 { 280 if(!validateEmailv2(objValue.value)) 281 { 282 if(!strError || strError.length ==0) 283 { 284 strError = objValue.name+": VyplÅte platnou e-mailovou adresu "; 285 }//if 286 alert(strError); 287 return false; 288 }//if 289 break; 290 }//case email 291 case "lt": 292 case "lessthan": 293 { 294 if(isNaN(objValue.value)) 295 { 296 alert(objValue.name+": Musà být ÄÃslo "); 297 return false; 298 }//if 299 if(eval(objValue.value) >= eval(cmdvalue)) 300 { 301 if(!strError || strError.length ==0) 302 { 303 strError = objValue.name + ": ÄÃslo musà být menšà než "+ cmdvalue; 304 }//if 305 alert(strError); 306 return false; 307 }//if 308 break; 309 }//case lessthan 310 case "gt": 311 case "greaterthan": 312 { 313 if(isNaN(objValue.value)) 314 { 315 alert(objValue.name+": Musà být ÄÃslo "); 316 return false; 317 }//if 318 if(eval(objValue.value) <= eval(cmdvalue)) 319 { 320 if(!strError || strError.length ==0) 321 { 322 strError = objValue.name + ": ÄÃslo musà být vÄtšà než "+ cmdvalue; 323 }//if 324 alert(strError); 325 return false; 326 }//if 327 break; 328 }//case greaterthan 329 case "regexp": 330 { 331 if(objValue.value.length > 0) 332 { 333 if(!objValue.value.match(cmdvalue)) 334 { 335 if(!strError || strError.length ==0) 336 { 337 strError = objValue.name+": Obsahuje nepovolený znak "; 338 }//if 339 alert(strError); 340 return false; 341 }//if 342 } 343 break; 344 }//case regexp 345 case "dontselect": 346 { 347 if(objValue.selectedIndex == null) 348 {
349 alert("VnitÅnà chyba - kontrola seznamu pro objekt, který nenà seznam."); 350 return false; 351 } 352 if(objValue.selectedIndex == eval(cmdvalue)) 353 { 354 if(!strError || strError.length ==0) 355 { 356 strError = objValue.name+": Vyberte jednu možnost "; 357 }//if 358 alert(strError); 359 return false; 360 } 361 break; 362 }//case dontselect 363 }//switch 364 return true; 365} 366/* 367 Copyright 2003 JavaScript-coder.com. All rights reserved. 368*/
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.