1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
/*
-------------------------------------------------------------------------
JavaScript Form Validator (gen_validatorv31.js)
Version 3.1.2
Copyright (C) 2003-2008 JavaScript-Coder.com. All rights reserved.
You can freely use this script in your Web pages.
You may adapt this script for your own needs, provided these opening credit
lines are kept intact.
The Form validation script is distributed free from JavaScript-Coder.com
For updates, please visit:
http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
Questions & comments please send to form.val at javascript-coder.com
-------------------------------------------------------------------------
*/
function Validator(frmname)
{
this.formobj=document.forms[frmname];
if(!this.formobj)
{
alert("Error: couldnot get Form object "+frmname);
return;
}
if(this.formobj.onsubmit)
{
this.formobj.old_onsubmit = this.formobj.onsubmit;
this.formobj.onsubmit=null;
}
else
{
this.formobj.old_onsubmit = null;
}
this.formobj._sfm_form_name=frmname;
this.formobj.onsubmit=form_submit_handler;
this.addValidation = add_validation;
this.setAddnlValidationFunction=set_addnl_vfunction;
this.clearAllValidations = clear_all_validations;
this.disable_validations = false;//new
document.error_disp_handler = new sfm_ErrorDisplayHandler();
this.EnableOnPageErrorDisplay=validator_enable_OPED;
this.EnableOnPageErrorDisplaySingleBox=validator_enable_OPED_SB;
this.show_errors_together=true;
this.EnableMsgsTogether=sfm_enable_show_msgs_together;
document.set_focus_onerror=true;
this.EnableFocusOnError=sfm_validator_enable_focus;
}
function sfm_validator_enable_focus(enable)
{
document.set_focus_onerror = enable;
}
function set_addnl_vfunction(functionname)
{
this.formobj.addnlvalidation = functionname;
}
function sfm_set_focus(objInput)
{
if(document.set_focus_onerror)
{
objInput.focus();
}
}
function sfm_enable_show_msgs_together()
{
this.show_errors_together=true;
this.formobj.show_errors_together=true;
}
function clear_all_validations()
{
for(var itr=0;itr < this.formobj.elements.length;itr++)
{
this.formobj.elements[itr].validationset = null;
}
}
function form_submit_handler()
{
var bRet = true;
document.error_disp_handler.clear_msgs();
for(var itr=0;itr < this.elements.length;itr++)
{
if(this.elements[itr].validationset &&
!this.elements[itr].validationset.validate())
{
bRet = false;
}
if(!bRet && !this.show_errors_together)
{
break;
}
}
if(this.addnlvalidation)
{
str =" var ret = "+this.addnlvalidation+"()";
eval(str);
if(!ret)
{
bRet=false;
}
}
if(!bRet)
{
document.error_disp_handler.FinalShowMsg();
return false;
}
return true;
}
function add_validation(itemname,descriptor,errstr)
{
var condition = null;
if(arguments.length > 3)
{
condition = arguments[3];
}
if(!this.formobj)
{
alert("Error: The form object is not set properly");
return;
}//if
var itemobj = this.formobj[itemname];
if(itemobj.length && isNaN(itemobj.selectedIndex) )
//for radio button; don't do for 'select' item
{
itemobj = itemobj[0];
}
if(!itemobj)
{
alert("Error: Couldnot get the input object named: "+itemname);
return;
}
if(!itemobj.validationset)
{
itemobj.validationset = new ValidationSet(itemobj,this.show_errors_together);
}
itemobj.validationset.add(descriptor,errstr,condition);
itemobj.validatorobj=this;
}
function validator_enable_OPED()
{
document.error_disp_handler.EnableOnPageDisplay(false);
}
function validator_enable_OPED_SB()
{
document.error_disp_handler.EnableOnPageDisplay(true);
}
function sfm_ErrorDisplayHandler()
{
this.msgdisplay = new AlertMsgDisplayer();
this.EnableOnPageDisplay= edh_EnableOnPageDisplay;
this.ShowMsg=edh_ShowMsg;
this.FinalShowMsg=edh_FinalShowMsg;
this.all_msgs=new Array();
this.clear_msgs=edh_clear_msgs;
}
function edh_clear_msgs()
{
this.msgdisplay.clearmsg(this.all_msgs);
this.all_msgs = new Array();
}
function edh_FinalShowMsg()
{
this.msgdisplay.showmsg(this.all_msgs);
}
function edh_EnableOnPageDisplay(single_box)
{
if(true == single_box)
{
this.msgdisplay = new SingleBoxErrorDisplay();
}
else
{
this.msgdisplay = new DivMsgDisplayer();
}
}
function edh_ShowMsg(msg,input_element)
{
var objmsg = new Array();
objmsg["input_element"] = input_element;
objmsg["msg"] = msg;
this.all_msgs.push(objmsg);
}
function AlertMsgDisplayer()
{
this.showmsg = alert_showmsg;
this.clearmsg=alert_clearmsg;
}
function alert_clearmsg(msgs)
{
}
function alert_showmsg(msgs)
{
var whole_msg="";
var first_elmnt=null;
for(var m=0;m < msgs.length;m++)
{
if(null == first_elmnt)
{
first_elmnt = msgs[m]["input_element"];
}
whole_msg += msgs[m]["msg"] + "\n";
}
alert(whole_msg);
if(null != first_elmnt)
{
sfm_set_focus(first_elmnt);
}
}
function sfm_show_error_msg(msg,input_elmt)
{
document.error_disp_handler.ShowMsg(msg,input_elmt);
}
function SingleBoxErrorDisplay()
{
this.showmsg=sb_div_showmsg;
this.clearmsg=sb_div_clearmsg;
}
function sb_div_clearmsg(msgs)
{
var divname = form_error_div_name(msgs);
show_div_msg(divname,"");
}
function sb_div_showmsg(msgs)
{
var whole_msg="<ul>\n";
for(var m=0;m < msgs.length;m++)
{
whole_msg += "<li>" + msgs[m]["msg"] + "</li>\n";
}
whole_msg += "</ul>";
var divname = form_error_div_name(msgs);
show_div_msg(divname,whole_msg);
}
function form_error_div_name(msgs)
{
var input_element= null;
for(var m in msgs)
{
input_element = msgs[m]["input_element"];
if(input_element){break;}
}
var divname ="";
if(input_element)
{
divname = input_element.form._sfm_form_name + "_errorloc";
}
return divname;
}
function DivMsgDisplayer()
{
this.showmsg=div_showmsg;
this.clearmsg=div_clearmsg;
}
function div_clearmsg(msgs)
{
for(var m in msgs)
{
var divname = element_div_name(msgs[m]["input_element"]);
show_div_msg(divname,"");
}
}
function element_div_name(input_element)
{
var divname = input_element.form._sfm_form_name + "_" +
input_element.name + "_errorloc";
divname = divname.replace(/[\[\]]/gi,"");
return divname;
}
function div_showmsg(msgs)
{
var whole_msg;
var first_elmnt=null;
for(var m in msgs)
{
if(null == first_elmnt)
{
first_elmnt = msgs[m]["input_element"];
}
var divname = element_div_name(msgs[m]["input_element"]);
show_div_msg(divname,msgs[m]["msg"]);
}
if(null != first_elmnt)
{
sfm_set_focus(first_elmnt);
}
}
function show_div_msg(divname,msgstring)
{
if(divname.length<=0) return false;
if(document.layers)
{
divlayer = document.layers[divname];
if(!divlayer){return;}
divlayer.document.open();
divlayer.document.write(msgstring);
divlayer.document.close();
}
else
if(document.all)
{
divlayer = document.all[divname];
if(!divlayer){return;}
divlayer.innerHTML=msgstring;
}
else
if(document.getElementById)
{
divlayer = document.getElementById(divname);
if(!divlayer){return;}
divlayer.innerHTML =msgstring;
}
divlayer.style.visibility="visible";
}
function ValidationDesc(inputitem,desc,error,condition)
{
this.desc=desc;
this.error=error;
this.itemobj = inputitem;
this.condition = condition;
this.validate=vdesc_validate;
}
function vdesc_validate()
{
if(this.condition != null )
{
if(!eval(this.condition))
{
return true;
}
}
if(!validateInput(this.desc,this.itemobj,this.error))
{
this.itemobj.validatorobj.disable_validations=true;
sfm_set_focus(this.itemobj);
return false;
}
return true;
}
function ValidationSet(inputitem,msgs_together)
{
this.vSet=new Array();
this.add= add_validationdesc;
this.validate= vset_validate;
this.itemobj = inputitem;
this.msgs_together = msgs_together;
}
function add_validationdesc(desc,error,condition)
{
this.vSet[this.vSet.length]=
new ValidationDesc(this.itemobj,desc,error,condition);
}
function vset_validate()
{
var bRet = true;
for(var itr=0;itr<this.vSet.length;itr++)
{
bRet = bRet && this.vSet[itr].validate();
if(!bRet && !this.msgs_together)
{
break;
}
}
return bRet;
}
function validateEmail(email)
{
var splitted = email.match("^(.+)@(.+)$");
if(splitted == null) return false;
if(splitted[1] != null )
{
var regexp_user=/^\"?[\w-_\.]*\"?$/;
if(splitted[1].match(regexp_user) == null) return false;
}
if(splitted[2] != null)
{
var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
if(splitted[2].match(regexp_domain) == null)
{
var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
if(splitted[2].match(regexp_ip) == null) return false;
}// if
return true;
}
return false;
}
function IsCheckSelected(objValue,chkValue)
{
var selected=false;
var objcheck = objValue.form.elements[objValue.name];
if(objcheck.length)
{
var idxchk=-1;
for(var c=0;c < objcheck.length;c++)
{
if(objcheck[c].value == chkValue)
{
idxchk=c;
break;
}//if
}//for
if(idxchk>= 0)
{
if(objcheck[idxchk].checked=="1")
{
selected=true;
}
}//if
}
else
{
if(objValue.checked == "1")
{
selected=true;
}//if
}//else
return selected;
}
function TestDontSelectChk(objValue,chkValue,strError)
{
var pass = true;
pass = IsCheckSelected(objValue,chkValue)?false:true;
if(pass==false)
{
if(!strError || strError.length ==0)
{
strError = "Can't Proceed as you selected "+objValue.name;
}//if
sfm_show_error_msg(strError,objValue);
}
return pass;
}
function TestShouldSelectChk(objValue,chkValue,strError)
{
var pass = true;
pass = IsCheckSelected(objValue,chkValue)?true:false;
if(pass==false)
{
if(!strError || strError.length ==0)
{
strError = "You should select "+objValue.name;
}//if
sfm_show_error_msg(strError,objValue);
}
return pass;
}
function TestRequiredInput(objValue,strError)
{
var ret = true;
var val = objValue.value;
val = val.replace(/^\s+|\s+$/g,"");//trim
if(eval(val.length) == 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : Required Field";
}//if
sfm_show_error_msg(strError,objValue);
ret=false;
}//if
return ret;
}
function TestMaxLen(objValue,strMaxLen,strError)
{
var ret = true;
if(eval(objValue.value.length) > eval(strMaxLen))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : "+ strMaxLen +" characters maximum ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestMinLen(objValue,strMinLen,strError)
{
var ret = true;
if(eval(objValue.value.length) < eval(strMinLen))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : " + strMinLen + " characters minimum ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestInputType(objValue,strRegExp,strError,strDefaultError)
{
var ret = true;
var charpos = objValue.value.search(strRegExp);
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = strDefaultError;
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestEmail(objValue,strError)
{
var ret = true;
if(objValue.value.length > 0 && !validateEmail(objValue.value) )
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Enter a valid Email address ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestLessThan(objValue,strLessThan,strError)
{
var ret = true;
if(isNaN(objValue.value))
{
sfm_show_error_msg(objValue.name +": Should be a number ",objValue);
ret = false;
}//if
else
if(eval(objValue.value) >= eval(strLessThan))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : value should be less than "+ strLessThan;
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestGreaterThan(objValue,strGreaterThan,strError)
{
var ret = true;
if(isNaN(objValue.value))
{
sfm_show_error_msg(objValue.name+": Should be a number ",objValue);
ret = false;
}//if
else
if(eval(objValue.value) <= eval(strGreaterThan))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : value should be greater than "+ strGreaterThan;
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestRegExp(objValue,strRegExp,strError)
{
var ret = true;
if( objValue.value.length > 0 &&
!objValue.value.match(strRegExp) )
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Invalid characters found ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestDontSelect(objValue,dont_sel_index,strError)
{
var ret = true;
if(objValue.selectedIndex == null)
{
sfm_show_error_msg("ERROR: dontselect command for non-select Item");
ret = false;
}
if(objValue.selectedIndex == eval(dont_sel_index))
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Please Select one option ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}
return ret;
}
function TestSelectOneRadio(objValue,strError)
{
var objradio = objValue.form.elements[objValue.name];
var one_selected=false;
for(var r=0;r < objradio.length;r++)
{
if(objradio[r].checked)
{
one_selected=true;
break;
}
}
if(false == one_selected)
{
if(!strError || strError.length ==0)
{
strError = "Please select one option from "+objValue.name;
}
sfm_show_error_msg(strError,objValue);
}
return one_selected;
}
function validateInput(strValidateStr,objValue,strError)
{
var ret = true;
var epos = strValidateStr.search("=");
var command = "";
var cmdvalue = "";
if(epos >= 0)
{
command = strValidateStr.substring(0,epos);
cmdvalue = strValidateStr.substr(epos+1);
}
else
{
command = strValidateStr;
}
switch(command)
{
case "req":
case "required":
{
ret = TestRequiredInput(objValue,strError)
break;
}//case required
case "maxlength":
case "maxlen":
{
ret = TestMaxLen(objValue,cmdvalue,strError)
break;
}//case maxlen
case "minlength":
case "minlen":
{
ret = TestMinLen(objValue,cmdvalue,strError)
break;
}//case minlen
case "alnum":
case "alphanumeric":
{
ret = TestInputType(objValue,"[^A-Za-z0-9]",strError,
objValue.name+": Only alpha-numeric characters allowed ");
break;
}
case "alnum_s":
case "alphanumeric_space":
{
ret = TestInputType(objValue,"[^A-Za-z0-9\\s]",strError,
objValue.name+": Only alpha-numeric characters and space allowed ");
break;
}
case "num":
case "numeric":
{
ret = TestInputType(objValue,"[^0-9]",strError,
objValue.name+": Only digits allowed ");
break;
}
case "dec":
case "decimal":
{
ret = TestInputType(objValue,"[^0-9\.]",strError,
objValue.name+": Only numbers allowed ");
break;
}
case "alphabetic":
case "alpha":
{
ret = TestInputType(objValue,"[^A-Za-z]",strError,
objValue.name+": Only alphabetic characters allowed ");
break;
}
case "alphabetic_space":
case "alpha_s":
{
ret = TestInputType(objValue,"[^A-Za-z\\s]",strError,
objValue.name+": Only alphabetic characters and space allowed ");
break;
}
case "email":
{
ret = TestEmail(objValue,strError);
break;
}
case "lt":
case "lessthan":
{
ret = TestLessThan(objValue,cmdvalue,strError);
break;
}
case "gt":
case "greaterthan":
{
ret = TestGreaterThan(objValue,cmdvalue,strError);
break;
}//case greaterthan
case "regexp":
{
ret = TestRegExp(objValue,cmdvalue,strError);
break;
}
case "dontselect":
{
ret = TestDontSelect(objValue,cmdvalue,strError)
break;
}
case "dontselectchk":
{
ret = TestDontSelectChk(objValue,cmdvalue,strError)
break;
}
case "shouldselchk":
{
ret = TestShouldSelectChk(objValue,cmdvalue,strError)
break;
}
case "selone_radio":
{
ret = TestSelectOneRadio(objValue,strError);
break;
}
}//switch
return ret;
}
function VWZ_IsListItemSelected(listname,value)
{
for(var i=0;i < listname.options.length;i++)
{
if(listname.options[i].selected == true &&
listname.options[i].value == value)
{
return true;
}
}
return false;
}
function VWZ_IsChecked(objcheck,value)
{
if(objcheck.length)
{
for(var c=0;c < objcheck.length;c++)
{
if(objcheck[c].checked == "1" &&
objcheck[c].value == value)
{
return true;
}
}
}
else
{
if(objcheck.checked == "1" )
{
return true;
}
}
return false;
}
/*
Copyright (C) 2003-2009 JavaScript-Coder.com . All rights reserved.
*/
|