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("BUG: couldnot get Form object "+frmname); 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 } 73 return true; 74} 75function add_validation(itemname,descriptor,errstr) 76{ 77 if(!this.formobj) 78 { 79 alert("BUG: the form object is not set properly"); 80 return; 81 }//if 82 var itemobj = this.formobj[itemname]; 83 if(!itemobj) 84 { 85 alert("BUG: Couldnot get the input object named: "+itemname); 86 return; 87 } 88 if(!itemobj.validationset) 89 { 90 itemobj.validationset = new ValidationSet(itemobj); 91 } 92 itemobj.validationset.add(descriptor,errstr); 93} 94function ValidationDesc(inputitem,desc,error) 95{ 96 this.desc=desc; 97 this.error=error; 98 this.itemobj = inputitem; 99 this.validate=vdesc_validate; 100} 101function vdesc_validate() 102{ 103 if(!V2validateData(this.desc,this.itemobj,this.error)) 104 { 105 this.itemobj.focus(); 106 return false; 107 } 108 return true; 109} 110function ValidationSet(inputitem) 111{ 112 this.vSet=new Array(); 113 this.add= add_validationdesc; 114 this.validate= vset_validate; 115 this.itemobj = inputitem; 116} 117function add_validationdesc(desc,error) 118{ 119 this.vSet[this.vSet.length]= 120 new ValidationDesc(this.itemobj,desc,error); 121} 122function vset_validate() 123{ 124 for(var itr=0;itr<this.vSet.length;itr++) 125 { 126 if(!this.vSet[itr].validate()) 127 { 128 return false; 129 } 130 } 131 return true; 132} 133function validateEmailv2(email) 134{ 135// a very simple email validation checking. 136// you can add more complex email checking if it helps 137 if(email.length <= 0) 138 { 139 return true; 140 } 141 var splitted = email.match("^(.+)@(.+)$"); 142 if(splitted == null) return false; 143 if(splitted[1] != null ) 144 { 145 var regexp_user=/^\"?[\w-_\.]*\"?$/; 146 if(splitted[1].match(regexp_user) == null) return false; 147 } 148 if(splitted[2] != null) 149 { 150 var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/; 151 if(splitted[2].match(regexp_domain) == null) 152 { 153 var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/; 154 if(splitted[2].match(regexp_ip) == null) return false; 155 }// if 156 return true; 157 } 158return false; 159} 160function V2validateData(strValidateStr,objValue,strError) 161{ 162 var epos = strValidateStr.search("="); 163 var command = ""; 164 var cmdvalue = ""; 165 if(epos >= 0) 166 { 167 command = strValidateStr.substring(0,epos); 168 cmdvalue = strValidateStr.substr(epos+1); 169 } 170 else 171 { 172 command = strValidateStr; 173 } 174 switch(command) 175 { 176 case "req": 177 case "required": 178 { 179 if(eval(objValue.value.length) == 0) 180 { 181 if(!strError || strError.length ==0) 182 { 183 strError = objValue.name + " : Required Field"; 184 }//if 185 alert(strError); 186 return false; 187 }//if 188 break; 189 }//case required 190 case "maxlength": 191 case "maxlen": 192 { 193 if(eval(objValue.value.length) > eval(cmdvalue)) 194 { 195 if(!strError || strError.length ==0) 196 { 197 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 198 }//if 199 alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 200 return false; 201 }//if 202 break; 203 }//case maxlen 204 case "minlength": 205 case "minlen": 206 { 207 if(eval(objValue.value.length) < eval(cmdvalue)) 208 { 209 if(!strError || strError.length ==0) 210 { 211 strError = objValue.name + " : " + cmdvalue + " characters minimum "; 212 }//if 213 alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 214 return false; 215 }//if 216 break; 217 }//case minlen 218 case "alnum": 219 case "alphanumeric": 220 { 221 var charpos = objValue.value.search("[^A-Za-z0-9]"); 222 if(objValue.value.length > 0 && charpos >= 0) 223 { 224 if(!strError || strError.length ==0) 225 { 226 strError = objValue.name+": Only alpha-numeric characters allowed "; 227 }//if 228 alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 229 return false; 230 }//if 231 break; 232 }//case alphanumeric 233 case "num": 234 case "numeric": 235 { 236 var charpos = objValue.value.search("[^0-9]"); 237 if(objValue.value.length > 0 && charpos >= 0) 238 { 239 if(!strError || strError.length ==0) 240 { 241 strError = objValue.name+": Only digits allowed "; 242 }//if 243 alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 244 return false; 245 }//if 246 break; 247 }//numeric 248 case "alphabetic": 249 case "alpha": 250 { 251 var charpos = objValue.value.search("[^A-Za-z]"); 252 if(objValue.value.length > 0 && charpos >= 0) 253 { 254 if(!strError || strError.length ==0) 255 { 256 strError = objValue.name+": Only alphabetic characters allowed "; 257 }//if 258 alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 259 return false; 260 }//if 261 break; 262 }//alpha 263 case "alnumhyphen": 264 { 265 var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 266 if(objValue.value.length > 0 && charpos >= 0) 267 { 268 if(!strError || strError.length ==0) 269 { 270 strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 271 }//if 272 alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 273 return false; 274 }//if 275 break; 276 } 277 case "email": 278 { 279 if(!validateEmailv2(objValue.value)) 280 { 281 if(!strError || strError.length ==0) 282 { 283 strError = objValue.name+": Enter a valid Email address "; 284 }//if 285 alert(strError); 286 return false; 287 }//if 288 break; 289 }//case email 290 case "lt": 291 case "lessthan": 292 { 293 if(isNaN(objValue.value)) 294 { 295 alert(objValue.name+": Should be a number "); 296 return false; 297 }//if 298 if(eval(objValue.value) >= eval(cmdvalue)) 299 { 300 if(!strError || strError.length ==0) 301 { 302 strError = objValue.name + " : value should be less than "+ cmdvalue; 303 }//if 304 alert(strError); 305 return false; 306 }//if 307 break; 308 }//case lessthan 309 case "gt": 310 case "greaterthan": 311 { 312 if(isNaN(objValue.value)) 313 { 314 alert(objValue.name+": Should be a number "); 315 return false; 316 }//if 317 if(eval(objValue.value) <= eval(cmdvalue)) 318 { 319 if(!strError || strError.length ==0) 320 { 321 strError = objValue.name + " : value should be greater than "+ cmdvalue; 322 }//if 323 alert(strError); 324 return false; 325 }//if 326 break; 327 }//case greaterthan 328 case "regexp": 329 { 330 if(objValue.value.length > 0) 331 { 332 if(!objValue.value.match(cmdvalue)) 333 { 334 if(!strError || strError.length ==0) 335 { 336 strError = objValue.name+": Invalid characters found "; 337 }//if 338 alert(strError); 339 return false; 340 }//if 341 } 342 break; 343 }//case regexp 344 case "dontselect": 345 { 346 if(objValue.selectedIndex == null) 347 { 348 alert("BUG: dontselect command for non-select Item"); 349 return false; 350 } 351 if(objValue.selectedIndex == eval(cmdvalue)) 352 { 353 if(!strError || strError.length ==0) 354 { 355 strError = objValue.name+": Please Select one option "; 356 }//if 357 alert(strError); 358 return false; 359 } 360 break; 361 }//case dontselect 362 }//switch 363 return true; 364} 365/* 366 Copyright 2003 JavaScript-coder.com. All rights reserved. 367*/
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.