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*/ 24 25var lastErrorObj = null; 26 27function Validator(frmname) 28{ 29 this.formobj=document.forms[frmname]; 30 if(!this.formobj) 31 { 32 alert("BUG: couldnot get Form object "+frmname); 33 return; 34 } 35 36 37/* if(this.formobj.onsubmit) 38 { 39 this.formobj.old_onsubmit = this.formobj.onsubmit; 40 this.formobj.onsubmit=null; 41 } 42 else 43 { 44 this.formobj.old_onsubmit = null; 45 } 46 47 this.formobj.onsubmit=form_submit_handler; 48*/ 49 this.validate = validate; 50 this.addValidation = add_validation; 51 this.setAddnlValidationFunction=set_addnl_vfunction; 52 this.clearAllValidations = clear_all_validations; 53 this.getLastErrorField = get_lastErrorField; 54} 55 56function set_addnl_vfunction(functionname) 57{ 58 this.formobj.addnlvalidation = functionname; 59} 60 61function clear_all_validations() 62{ 63 for(var itr=0;itr < this.formobj.elements.length;itr++) 64 { 65 this.formobj.elements[itr].validationset = null; 66 } 67} 68 69function validate () 70{ 71 return (form_submit_handler (this.formobj)); 72} 73 74function form_submit_handler(form) 75{ 76 if (form == undefined) 77 form = this; 78 79 for(var itr=0;itr < form.elements.length;itr++) 80 { 81 if(form.elements[itr].validationset && 82 !form.elements[itr].validationset.validate()) 83 { 84 return false; 85 } 86 } 87 if(this.addnlvalidation) 88 { 89 str =" var ret = "+this.addnlvalidation+"()"; 90 eval(str); 91 if(!ret) return ret; 92 } 93 return true; 94} 95 96function add_validation (itemname,descriptor,errstr,emptystr) 97{ 98 if(!this.formobj) 99 { 100 alert("BUG: the form object is not set properly"); 101 return; 102 } 103 104 var itemobj = this.formobj[itemname]; 105 if(!itemobj) 106 { 107 alert("BUG: Could not get the input object named: "+itemname); 108 return; 109 } 110 111 if(!itemobj.validationset) 112 { 113 itemobj.validationset = new ValidationSet(itemobj); 114 } 115 116 itemobj.validationset.add(descriptor,errstr,emptystr); 117} 118 119function ValidationDesc (inputitem,desc,error,empty) 120{ 121 this.desc=desc; 122 this.error=error; 123 this.empty=empty; 124 this.itemobj = inputitem; 125 this.validate=vdesc_validate; 126} 127 128function vdesc_validate() 129{ 130 if(!V2validateData(this.desc,this.itemobj,this.error,this.empty)) 131 { 132 this.itemobj.focus(); 133 lastErrorObj = this.itemobj; 134 return false; 135 } 136 return true; 137} 138 139function get_lastErrorField () 140{ 141 return lastErrorObj; 142} 143 144 145function ValidationSet(inputitem) 146{ 147 this.vSet=new Array(); 148 this.add= add_validationdesc; 149 this.validate= vset_validate; 150 this.itemobj = inputitem; 151} 152 153function add_validationdesc(desc,error,empty) 154{ 155 this.vSet[this.vSet.length]= 156 new ValidationDesc(this.itemobj,desc,error,empty); 157} 158 159function vset_validate() 160{ 161 for(var itr=0;itr<this.vSet.length;itr++) 162 { 163 if(!this.vSet[itr].validate()) 164 { 165 return false; 166 } 167 } 168 return true; 169} 170 171function validateEmailv2(email) 172{ 173 // a very simple email validation checking. 174 // you can add more complex email checking if it helps 175 176 if(email.length <= 0) 177 { 178 return true; 179 } 180 181 var splitted = email.match(/^(.+)@(.+)$/); 182 if(splitted == null) return false; 183 if(splitted[1] != null ) 184 { 185 var regexp_user=/^\"?[\w-_\.]*\"?$/; 186 if(splitted[1].match(regexp_user) == null) return false; 187 } 188 if(splitted[2] != null) 189 { 190 var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/; 191 if(splitted[2].match(regexp_domain) == null) 192 { 193 var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/; 194 if(splitted[2].match(regexp_ip) == null) return false;
195 }// if 196 return true; 197 } 198 return false; 199} 200 201function TZValidator(id) 202{ 203 var sID = String(id); 204 if ((sID.length != 9) || (isNaN(sID))) 205 return false; 206 var counter = 0, incNum; 207 for (var i = 0; i < 9; i++) 208 { 209 incNum = Number(sID.charAt(i)); 210 incNum *= (i % 2) + 1; 211 if (incNum > 9) 212 incNum -= 9; 213 counter += incNum; 214 } 215 return (counter % 10 == 0); 216} 217 218function V2validateData(strValidateStr,objValue,strError,strEmpty) 219{ 220 var epos = strValidateStr.indexOf("="); 221 var command = ""; 222 var cmdvalue = ""; 223 if(epos >= 0) 224 { 225 command = strValidateStr.substring(0,epos); 226 cmdvalue = strValidateStr.substr(epos+1); 227 } 228 else 229 { 230 command = strValidateStr; 231 } 232 switch(command) 233 { 234 case "req": 235 case "required": 236 { 237 if(eval(objValue.value.length) == 0 || (strEmpty != "" && objValue.value == strEmpty)) 238 { 239 if(!strError || strError.length ==0) 240 { 241 strError = objValue.name + " : Required Field"; 242 }//if 243 objValue.focus(); 244 alert(strError); 245 return false; 246 }//if 247 break; 248 }//case required 249 case "maxlength": 250 case "maxlen": 251 { 252 if(eval(objValue.value.length) > eval(cmdvalue)) 253 { 254 if(!strError || strError.length ==0) 255 { 256 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 257 }//if 258 objValue.focus(); 259 alert(strError); // + "\n[Current length = " + objValue.value.length + " ]"); 260 return false; 261 }//if 262 break; 263 }//case maxlen 264 case "minlength": 265 case "minlen": 266 { 267 if(eval(objValue.value.length) < eval(cmdvalue) && eval(objValue.value.length) > 0) 268 { 269 if(!strError || strError.length ==0) 270 { 271 strError = objValue.name + " : " + cmdvalue + " characters minimum "; 272 }//if 273 objValue.focus(); 274 alert(strError); // + "\n[Current length = " + objValue.value.length + " ]"); 275 return false; 276 }//if 277 break; 278 }//case minlen 279 case "alnum": 280 case "alphanumeric": 281 { 282 var charpos = objValue.value.search(/^\w+$/); 283 if(objValue.value.length > 0 && charpos == -1) 284 { 285 if(!strError || strError.length ==0) 286 { 287 strError = objValue.name+": Only alpha-numeric characters allowed "; 288 }//if 289 objValue.focus(); 290 alert(strError); // + "\n [Error character position " + eval(charpos+1)+"]"); 291 return false; 292 }//if 293 break; 294 }//case alphanumeric 295 case "num": 296 case "numeric": 297 { 298 var charpos = objValue.value.search(/^\d+$/); 299 if(objValue.value.length > 0 && charpos == -1) 300 { 301 if(!strError || strError.length ==0) 302 { 303 strError = objValue.name+": Only digits allowed "; 304 }//if 305 objValue.focus(); 306 alert(strError); 307 //+ "\n [Error character position " + eval(charpos+1)+"]"); 308 return false; 309 }//if 310 break; 311 }//numeric 312 case "phone": 313 { 314 if (objValue.value == "" || (strEmpty != "" && objValue.value == strEmpty)) return true; 315 if (objValue.value.indexOf("-") != -1) 316 { 317 regexp1 = new RegExp("0[23489]-[0-9]{7}$"); 318 regexp2 = new RegExp("05[0-9]-[0-9]{7}$"); 319 regexp3 = new RegExp("07-[0-9]{8}$"); 320 regexp4 = new RegExp("077-[0-9]{7}$"); 321 regexp5 = new RegExp("01[2789]-[0-9]{7}$"); 322 regexp6 = new RegExp("1-700-[0-9]{6}$"); 323 regexp7 = new RegExp("1-800-[0-9]{6}$"); 324 regexp8 = new RegExp("1-700-[0-9]{2}-[0-9]{2}-[0-9]{2}$"); 325 regexp9 = new RegExp("1-800-[0-9]{2}-[0-9]{2}-[0-9]{2}$"); 326 regexp10 = new RegExp("1-700-[0-9]{3}-[0-9]{3}$"); 327 regexp11 = new RegExp("1-800-[0-9]{3}-[0-9]{3}$"); 328 329 if (!regexp1.test(objValue.value) && 330 !regexp2.test(objValue.value) && 331 !regexp3.test(objValue.value) && 332 !regexp4.test(objValue.value) && 333 !regexp5.test(objValue.value) && 334 !regexp6.test(objValue.value) && 335 !regexp7.test(objValue.value) && 336 !regexp8.test(objValue.value) && 337 !regexp9.test(objValue.value) && 338 !regexp10.test(objValue.value) && 339 !regexp11.test(objValue.value)) 340 { 341 objValue.focus(); 342 alert(strError); 343 return false; 344 } 345 } 346 else 347 {
348 regexp1 = new RegExp("0[234895][0-9]{7}$"); 349 regexp2 = new RegExp("0[57][0-9]{8}$"); 350 regexp3 = new RegExp("01[2789][0-9]{7}$"); 351 regexp4 = new RegExp("1[78]00[0-9]{6}$"); 352 353 if (!regexp1.test(objValue.value) && 354 !regexp2.test(objValue.value) && 355 !regexp3.test(objValue.value) && 356 !regexp4.test(objValue.value)) 357 { 358 objValue.focus(); 359 alert(strError); 360 return false; 361 } 362 } 363 break; 364 }//phone 365 case "homephone": 366 { 367 if (objValue.value == "") return true; 368 if (objValue.value.indexOf("-") != -1) 369 { 370 regexp1 = new RegExp("0[23489]-[0-9]{7}$"); 371 regexp2 = new RegExp("07-[0-9]{8}$"); 372 regexp3 = new RegExp("077-[0-9]{7}$"); 373 regexp4 = new RegExp("01[2789]-[0-9]{7}$"); 374 regexp5 = new RegExp("1-700-[0-9]{6}$"); 375 regexp6 = new RegExp("1-800-[0-9]{6}$"); 376 regexp7 = new RegExp("1-700-[0-9]{2}-[0-9]{2}-[0-9]{2}$"); 377 regexp8 = new RegExp("1-800-[0-9]{2}-[0-9]{2}-[0-9]{2}$"); 378 regexp9 = new RegExp("1-700-[0-9]{3}-[0-9]{3}$"); 379 regexp10 = new RegExp("1-800-[0-9]{3}-[0-9]{3}$"); 380 381 if (!regexp1.test(objValue.value) && 382 !regexp2.test(objValue.value) && 383 !regexp3.test(objValue.value) && 384 !regexp4.test(objValue.value) && 385 !regexp5.test(objValue.value) && 386 !regexp6.test(objValue.value) && 387 !regexp7.test(objValue.value) && 388 !regexp8.test(objValue.value) && 389 !regexp9.test(objValue.value) && 390 !regexp10.test(objValue.value)) 391 { 392 objValue.focus(); 393 alert(strError); 394 return false; 395 } 396 } 397 else 398 { 399 regexp1 = new RegExp("0[23489][0-9]{7}$"); 400 regexp2 = new RegExp("01[2789][0-9]{7}$"); 401 regexp3 = new RegExp("1[78]00[0-9]{6}$"); 402 regexp4 = new RegExp("077[0-9]{7}$"); 403 404 if (!regexp1.test(objValue.value) && 405 !regexp2.test(objValue.value) && 406 !regexp3.test(objValue.value) && 407 !regexp4.test(objValue.value)) 408 { 409 objValue.focus(); 410 alert(strError); 411 return false; 412 } 413 } 414 break; 415 }//phone 416 case "mobile": 417 { 418 if (objValue.value == "") return true; 419 if (objValue.value.indexOf("-") != -1) 420 { 421 regexp1 = new RegExp("05[0-9]-[0-9]{7}$"); 422 423 if (!regexp1.test(objValue.value)) 424 { 425 objValue.focus(); 426 alert(strError); 427 return false; 428 } 429 } 430 else 431 { 432 regexp1 = new RegExp("05[0-9]{7}$"); 433 regexp2 = new RegExp("0[57][0-9]{8}$"); 434 435 if (!regexp1.test(objValue.value) && 436 !regexp2.test(objValue.value)) 437 { 438 objValue.focus(); 439 alert(strError); 440 return false; 441 } 442 } 443 break; 444 }//phone 445 case "globalphone": 446 { 447 if (objValue.value == "") return true; 448 regexp1 = new RegExp("[0-9\-\+\.]+"); 449 if (!regexp1.test(objValue.value)) 450 { 451 objValue.focus(); 452 alert(strError); 453 return false; 454 } 455 break; 456 }//gloablphone 457 case "alphabetic": 458 case "alpha": 459 { 460 var regexp1 = new RegExp("[A-Za-z×-ת]+"); 461 var regexp2 = new RegExp("[0-9\-\+\.]+"); 462 if(objValue.value.length > 0 && (!regexp1.test(objValue.value) || regexp2.test(objValue.value))) 463 { 464 if(!strError || strError.length ==0) 465 { 466 strError = objValue.name+": Only alphabetic characters allowed "; 467 }//if 468 objValue.focus(); 469 alert(strError); 470 //+ "\n [Error character position " + eval(charpos+1)+"]"); 471 return false; 472 }//if 473 break; 474 }//alpha 475 case "alnumhyphen": 476 { 477 var regexp1 = new RegExp(/^[a-zA-Z0-9-_]+$/); 478 if(objValue.value.length > 0 && !regexp1.test(objValue.value)) 479 { 480 if(!strError || strError.length ==0) 481 { 482 strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 483 }//if 484 objValue.focus(); 485 alert(strError); 486 //+ "\n [Error character position " + eval(charpos+1)+"]"); 487 return false; 488 }//if 489 break; 490 } 491 case "onlyeng": 492 { 493 var regexp1 = new RegExp(/^[a-zA-Z-_ ]+$/); 494 if(objValue.value.length > 0 && !regexp1.test(objValue.value)) 495 { 496 if(!strError || strError.length ==0) 497 { 498 strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 499 }//if 500 objValue.focus(); 501 alert(strError); 502 //+ "\n [Error character position " + eval(charpos+1)+"]"); 503 return false; 504 }//if 505 break; 506 } 507 case "email": 508 { 509 if (strEmpty != "" && objValue.value == strEmpty) return true; 510 if(!validateEmailv2(objValue.value)) 511 { 512 if(!strError || strError.length ==0) 513 { 514 strError = objValue.name+": Enter a valid Email address "; 515 }//if 516 objValue.focus(); 517 alert(strError); 518 return false; 519 }//if 520 break; 521 }//case email 522 case "lt": 523 case "lessthan": 524 { 525 if(isNaN(objValue.value)) 526 { 527 objValue.focus(); 528 alert(objValue.name+": Should be a number "); 529 return false; 530 }//if 531 if(eval(objValue.value) >= eval(cmdvalue)) 532 { 533 if(!strError || strError.length ==0) 534 { 535 strError = objValue.name + " : value should be less than "+ cmdvalue; 536 }//if 537 objValue.focus(); 538 alert(strError); 539 return false; 540 }//if 541 break; 542 }//case lessthan 543 case "gt": 544 case "greaterthan": 545 { 546 if(isNaN(objValue.value)) 547 { 548 objValue.focus(); 549 alert(objValue.name+": Should be a number "); 550 return false; 551 }//if 552 if(eval(objValue.value) <= eval(cmdvalue)) 553 { 554 if(!strError || strError.length ==0) 555 { 556 strError = objValue.name + " : value should be greater than "+ cmdvalue; 557 } //if 558 objValue.focus(); 559 alert(strError); 560 return false; 561 }//if 562 break; 563 }//case greaterthan 564 case "regexp": 565 { 566 if(objValue.value.length > 0) 567 { 568 if(!objValue.value.match(cmdvalue)) 569 { 570 if(!strError || strError.length ==0) 571 { 572 strError = objValue.name+": Invalid characters found "; 573 }//if 574 objValue.focus(); 575 alert(strError); 576 return false; 577 }//if 578 } 579 break; 580 }//case regexp 581 case "dontselect": 582 { 583 if(objValue.selectedIndex == null) 584 { 585 objValue.focus(); 586 alert("BUG: dontselect command for non-select Item"); 587 return false; 588 } 589 if(objValue.selectedIndex == eval(cmdvalue)) 590 { 591 if(!strError || strError.length ==0) 592 { 593 strError = objValue.name+": Please Select one option "; 594 }//if 595 objValue.focus(); 596 alert(strError); 597 return false; 598 } 599 break; 600 }//case dontselect 601 case "tz": 602 { 603 if(!TZValidator(objValue.value)) 604 { 605 if(!strError || strError.length ==0) 606 { 607 strError = objValue.name+": Enter a valid TZ "; 608 }//if 609 objValue.focus(); 610 alert(strError); 611 return false; 612 }//if 613 break; 614 }//case tz 615 }//switch 616 return true; 617} 618/* 619 Copyright 2003 JavaScript-coder.com. All rights reserved. 620*/
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.