1/* 2 3 ------------------------------------------------------------------------- 4 5 JavaScript Form Validator (gen_validatorv4.js) 6 7 Version 4.0 8 9 Copyright (C) 2003-2011 JavaScript-Coder.com. All rights reserved. 10 11 You can freely use this script in your Web pages. 12 13 You may adapt this script for your own needs, provided these opening credit 14 15 lines are kept intact. 16 17 18 19 The Form validation script is distributed free from JavaScript-Coder.com 20 21 For updates, please visit: 22 23 http://www.javascript-coder.com/html-form/javascript-form-validation.phtml 24 25 26 27 Questions & comments please send to form.val (at) javascript-coder.com 28 29 ------------------------------------------------------------------------- 30 31*/ 32 33 34 35function Validator(frmname) 36 37{ 38 39 this.validate_on_killfocus = false; 40 41 this.formobj = document.forms[frmname]; 42 43 if (!this.formobj) 44 45 { 46 47 alert("Error: couldnot get Form object " + frmname); 48 49 return; 50 51 } 52 53 if (this.formobj.onsubmit) 54 55 { 56 57 this.formobj.old_onsubmit = this.formobj.onsubmit; 58 59 this.formobj.onsubmit = null; 60 61 } 62 63 else 64 65 { 66 67 this.formobj.old_onsubmit = null; 68 69 } 70 71 this.formobj._sfm_form_name = frmname; 72 73 74 75 this.formobj.onsubmit = form_submit_handler; 76 77 this.addValidation = add_validation; 78 79 80 81 this.formobj.addnlvalidations = new Array(); 82 83 this.addAddnlValidationFunction = add_addnl_vfunction; 84 85 this.formobj.runAddnlValidations = run_addnl_validations; 86 87 this.setAddnlValidationFunction = set_addnl_vfunction;//for backward compatibility 88 89 90 91 92 93 this.clearAllValidations = clear_all_validations; 94 95 this.focus_disable_validations = false; 96 97 98 99 document.error_disp_handler = new sfm_ErrorDisplayHandler(); 100 101 102 103 this.EnableOnPageErrorDisplay = validator_enable_OPED; 104 105 this.EnableOnPageErrorDisplaySingleBox = validator_enable_OPED_SB; 106 107 108 109 this.show_errors_together = false; 110 111 this.EnableMsgsTogether = sfm_enable_show_msgs_together; 112 113 document.set_focus_onerror = true; 114 115 this.EnableFocusOnError = sfm_validator_enable_focus; 116 117 118 119 this.formobj.error_display_loc = 'right'; 120 121 this.SetMessageDisplayPos = sfm_validator_message_disp_pos; 122 123 124 125 this.formobj.DisableValidations = sfm_disable_validations; 126 127 this.formobj.validatorobj = this; 128 129} 130 131 132 133 134 135function sfm_validator_enable_focus(enable) 136 137{ 138 139 document.set_focus_onerror = enable; 140 141} 142 143 144 145function add_addnl_vfunction() 146 147{ 148 149 var proc = 150 151 { 152 153 }; 154 155 proc.func = arguments[0]; 156 157 proc.arguments = []; 158 159 160 161 for (var i = 1; i < arguments.length; i++) 162 163 { 164 165 proc.arguments.push(arguments[i]); 166 167 } 168 169 this.formobj.addnlvalidations.push(proc); 170 171} 172 173 174 175function set_addnl_vfunction(functionname) 176 177{ 178 179 if(functionname.constructor == String) 180 181 { 182 183 alert("Pass the function name like this: validator.setAddnlValidationFunction(DoCustomValidation)\n "+ 184 185 "rather than passing the function name as string"); 186 187 return; 188 189 } 190 191 this.addAddnlValidationFunction(functionname); 192 193} 194 195 196 197function run_addnl_validations() 198 199{ 200 201 var ret = true; 202 203 for (var f = 0; f < this.addnlvalidations.length; f++) 204 205 { 206 207 var proc = this.addnlvalidations[f]; 208 209 var args = proc.arguments || []; 210 211 if (!proc.func.apply(null, args)) 212 213 { 214 215 ret = false; 216 217 } 218 219 } 220 221 return ret; 222 223} 224 225 226 227function sfm_set_focus(objInput) 228 229{ 230 231 if (document.set_focus_onerror) 232 233 { 234 235 if (!objInput.disabled && objInput.type != 'hidden') 236 237 { 238 239 objInput.focus(); 240 241 } 242 243 } 244 245} 246 247 248 249function sfm_disable_validations() 250 251{ 252 253 if (this.old_onsubmit) 254 255 { 256 257 this.onsubmit = this.old_onsubmit; 258 259 } 260 261 else 262 263 { 264 265 this.onsubmit = null; 266 267 } 268 269} 270 271 272 273function sfm_enable_show_msgs_together() 274 275{ 276 277 this.show_errors_together = true; 278 279 this.formobj.show_errors_together = true; 280 281} 282 283 284 285function sfm_validator_message_disp_pos(pos) 286 287{ 288 289 this.formobj.error_display_loc = pos; 290 291} 292 293 294 295function clear_all_validations() 296 297{ 298 299 for (var itr = 0; itr < this.formobj.elements.length; itr++) 300 301 { 302 303 this.formobj.elements[itr].validationset = null; 304 305 } 306 307} 308 309 310 311function form_submit_handler() 312 313{ 314 315 var bRet = true; 316 317 document.error_disp_handler.clear_msgs(); 318 319 for (var itr = 0; itr < this.elements.length; itr++) 320 321 { 322 323 if (this.elements[itr].validationset && !this.elements[itr].validationset.validate()) 324 325 { 326 327 bRet = false; 328 329 } 330 331 if (!bRet && !this.show_errors_together) 332 333 { 334 335 break; 336 337 } 338 339 } 340 341 342 343 if (this.show_errors_together || bRet && !this.show_errors_together) 344 345 { 346 347 if (!this.runAddnlValidations()) 348 349 { 350 351 bRet = false; 352 353 } 354 355 } 356 357 if (!bRet) 358 359 { 360 361 document.error_disp_handler.FinalShowMsg(); 362 363 return false; 364 365 } 366 367 return true; 368 369} 370 371 372 373function add_validation(itemname, descriptor, errstr) 374 375{ 376 377 var condition = null; 378 379 if (arguments.length > 3) 380 381 { 382 383 condition = arguments[3]; 384 385 } 386 387 if (!this.formobj) 388 389 { 390 391 alert("Error: The form object is not set properly"); 392 393 return; 394 395 } //if 396 397 var itemobj = this.formobj[itemname]; 398 399 400 401 if (itemobj.length && isNaN(itemobj.selectedIndex)) 402 403 //for radio button; don't do for 'select' item 404 405 { 406 407 itemobj = itemobj[0]; 408 409 } 410 411 if (!itemobj) 412 413 { 414 415 alert("Error: Couldnot get the input object named: " + itemname); 416 417 return; 418 419 } 420 421 if (true == this.validate_on_killfocus) 422 423 { 424 425 itemobj.onblur = handle_item_on_killfocus; 426 427 } 428
429 if (!itemobj.validationset) 430 431 { 432 433 itemobj.validationset = new ValidationSet(itemobj, this.show_errors_together); 434 435 } 436 437 itemobj.validationset.add(descriptor, errstr, condition); 438 439 itemobj.validatorobj = this; 440 441} 442 443 444 445function handle_item_on_killfocus() 446 447{ 448 449 if (this.validatorobj.focus_disable_validations == true) 450 451 { 452 453 /* 454 455 To avoid repeated looping message boxes 456 457 */ 458 459 this.validatorobj.focus_disable_validations = false; 460 461 return false; 462 463 } 464 465 466 467 if (null != this.validationset) 468 469 { 470 471 document.error_disp_handler.clear_msgs(); 472 473 if (false == this.validationset.validate()) 474 475 { 476 477 document.error_disp_handler.FinalShowMsg(); 478 479 return false; 480 481 } 482 483 } 484 485} 486 487 488 489function validator_enable_OPED() 490 491{ 492 493 document.error_disp_handler.EnableOnPageDisplay(false); 494 495} 496 497 498 499function validator_enable_OPED_SB() 500 501{ 502 503 document.error_disp_handler.EnableOnPageDisplay(true); 504 505} 506 507 508 509function sfm_ErrorDisplayHandler() 510 511{ 512 513 this.msgdisplay = new AlertMsgDisplayer(); 514 515 this.EnableOnPageDisplay = edh_EnableOnPageDisplay; 516 517 this.ShowMsg = edh_ShowMsg; 518 519 this.FinalShowMsg = edh_FinalShowMsg; 520 521 this.all_msgs = new Array(); 522 523 this.clear_msgs = edh_clear_msgs; 524 525} 526 527 528 529function edh_clear_msgs() 530 531{ 532 533 this.msgdisplay.clearmsg(this.all_msgs); 534 535 this.all_msgs = new Array(); 536 537} 538 539 540 541function edh_FinalShowMsg() 542 543{ 544 545 if (this.all_msgs.length == 0) 546 547 { 548 549 return; 550 551 } 552 553 this.msgdisplay.showmsg(this.all_msgs); 554 555} 556 557 558 559function edh_EnableOnPageDisplay(single_box) 560 561{ 562 563 if (true == single_box) 564 565 { 566 567 this.msgdisplay = new SingleBoxErrorDisplay(); 568 569 } 570 571 else 572 573 { 574 575 this.msgdisplay = new DivMsgDisplayer(); 576 577 } 578 579} 580 581 582 583function edh_ShowMsg(msg, input_element) 584 585{ 586 587 var objmsg = new Array(); 588 589 objmsg["input_element"] = input_element; 590 591 objmsg["msg"] = msg; 592 593 this.all_msgs.push(objmsg); 594 595} 596 597 598 599function AlertMsgDisplayer() 600 601{ 602 603 this.showmsg = alert_showmsg; 604 605 this.clearmsg = alert_clearmsg; 606 607} 608 609 610 611function alert_clearmsg(msgs) 612 613{ 614 615 616 617} 618 619 620 621function alert_showmsg(msgs) 622 623{ 624 625 var whole_msg = ""; 626 627 var first_elmnt = null; 628 629 for (var m = 0; m < msgs.length; m++) 630 631 { 632 633 if (null == first_elmnt) 634 635 { 636 637 first_elmnt = msgs[m]["input_element"]; 638 639 } 640 641 whole_msg += msgs[m]["msg"] + "\n"; 642 643 } 644 645 646 647 alert(whole_msg); 648 649 650 651 if (null != first_elmnt) 652 653 { 654 655 sfm_set_focus(first_elmnt); 656 657 } 658 659} 660 661 662 663function sfm_show_error_msg(msg, input_elmt) 664 665{ 666 667 document.error_disp_handler.ShowMsg(msg, input_elmt); 668 669} 670 671 672 673function SingleBoxErrorDisplay() 674 675{ 676 677 this.showmsg = sb_div_showmsg; 678 679 this.clearmsg = sb_div_clearmsg; 680 681} 682 683 684 685function sb_div_clearmsg(msgs) 686 687{ 688 689 var divname = form_error_div_name(msgs); 690 691 sfm_show_div_msg(divname, ""); 692 693} 694 695 696 697function sb_div_showmsg(msgs) 698 699{ 700 701 var whole_msg = "<ul>\n"; 702 703 for (var m = 0; m < msgs.length; m++) 704 705 { 706 707 whole_msg += "<li>" + msgs[m]["msg"] + "</li>\n"; 708 709 } 710 711 whole_msg += "</ul>"; 712 713 var divname = form_error_div_name(msgs); 714 715 var anc_name = divname + "_loc"; 716 717 whole_msg = "<a name='" + anc_name + "' >" + whole_msg; 718 719 720 721 sfm_show_div_msg(divname, whole_msg); 722 723 724 725 window.location.hash = anc_name; 726 727} 728 729 730 731function form_error_div_name(msgs) 732 733{ 734 735 var input_element = null; 736 737 738 739 for (var m in msgs) 740 741 { 742 743 input_element = msgs[m]["input_element"]; 744 745 if (input_element) 746 747 { 748 749 break; 750 751 } 752 753 } 754 755 756 757 var divname = ""; 758 759 if (input_element) 760 761 { 762 763 divname = input_element.form._sfm_form_name + "_errorloc"; 764 765 } 766 767 768 769 return divname; 770 771} 772 773 774 775function sfm_show_div_msg(divname,msgstring) 776 777{ 778 779 if(divname.length<=0) return false; 780 781 782 783 if(document.layers) 784 785 { 786 787 divlayer = document.layers[divname]; 788 789 if(!divlayer){return;} 790 791 divlayer.document.open(); 792 793 divlayer.document.write(msgstring); 794 795 divlayer.document.close(); 796 797 } 798 799 else 800 801 if(document.all) 802 803 { 804 805 divlayer = document.all[divname]; 806 807 if(!divlayer){return;} 808 809 divlayer.innerHTML=msgstring; 810 811 } 812 813 else 814 815 if(document.getElementById) 816 817 { 818 819 divlayer = document.getElementById(divname); 820 821 if(!divlayer){return;} 822 823 divlayer.innerHTML =msgstring; 824 825 } 826 827 divlayer.style.visibility="visible"; 828 829 return false; 830 831} 832 833 834 835function DivMsgDisplayer() 836 837{ 838 839 this.showmsg = div_showmsg; 840 841 this.clearmsg = div_clearmsg; 842 843} 844 845 846 847function div_clearmsg(msgs) 848 849{ 850 851 for (var m in msgs) 852 853 { 854 855 var divname = element_div_name(msgs[m]["input_element"]); 856 857 show_div_msg(divname, ""); 858 859 } 860 861} 862 863 864 865function element_div_name(input_element) 866 867{ 868 869 var divname = input_element.form._sfm_form_name + "_" + input_element.name + "_errorloc"; 870 871 872 873 divname = divname.replace(/[\[\]]/gi, ""); 874 875 876 877 return divname; 878 879} 880 881 882 883function div_showmsg(msgs) 884 885{ 886 887 var whole_msg; 888 889 var first_elmnt = null; 890 891 for (var m in msgs) 892 893 { 894 895 if (null == first_elmnt) 896 897 { 898 899 first_elmnt = msgs[m]["input_element"]; 900 901 } 902 903 var divname = element_div_name(msgs[m]["input_element"]); 904 905 show_div_msg(divname, msgs[m]["msg"]); 906 907 } 908 909 if (null != first_elmnt) 910 911 { 912 913 sfm_set_focus(first_elmnt); 914 915 } 916 917} 918 919 920 921function show_div_msg(divname, msgstring) 922 923{ 924 925 if (divname.length <= 0) return false; 926 927 928 929 if (document.layers) 930 931 { 932 933 divlayer = document.layers[divname]; 934 935 if (!divlayer) 936 937 { 938 939 return; 940 941 } 942 943 divlayer.document.open(); 944 945 divlayer.document.write(msgstring); 946 947 divlayer.document.close(); 948 949 } 950 951 else if (document.all) 952 953 { 954 955 divlayer = document.all[divname]; 956 957 if (!divlayer) 958 959 { 960 961 return; 962 963 } 964 965 divlayer.innerHTML = msgstring; 966 967 } 968 969 else if (document.getElementById) 970 971 { 972 973 divlayer = document.getElementById(divname); 974 975 if (!divlayer) 976 977 { 978 979 return; 980 981 } 982 983 divlayer.innerHTML = msgstring; 984 985 } 986 987 divlayer.style.visibility = "visible"; 988 989} 990 991 992 993function ValidationDesc(inputitem, desc, error, condition) 994 995{ 996 997 this.desc = desc; 998 999 this.error = error; 1000 1001 this.itemobj = inputitem; 1002 1003 this.condition = condition; 1004 1005 this.validate = vdesc_validate; 1006 1007} 1008 1009 1010 1011function vdesc_validate() 1012 1013{ 1014 1015 if (this.condition != null) 1016 1017 { 1018 1019 if (!eval(this.condition)) 1020 1021 { 1022 1023 return true; 1024 1025 } 1026 1027 } 1028 1029 if (!validateInput(this.desc, this.itemobj, this.error)) 1030 1031 { 1032
1033 this.itemobj.validatorobj.focus_disable_validations = true; 1034 1035 sfm_set_focus(this.itemobj); 1036 1037 return false; 1038 1039 } 1040 1041 1042 1043 return true; 1044 1045} 1046 1047 1048 1049function ValidationSet(inputitem, msgs_together) 1050 1051{ 1052 1053 this.vSet = new Array(); 1054 1055 this.add = add_validationdesc; 1056 1057 this.validate = vset_validate; 1058 1059 this.itemobj = inputitem; 1060 1061 this.msgs_together = msgs_together; 1062 1063} 1064 1065 1066 1067function add_validationdesc(desc, error, condition) 1068 1069{ 1070 1071 this.vSet[this.vSet.length] = 1072 1073 new ValidationDesc(this.itemobj, desc, error, condition); 1074 1075} 1076 1077 1078 1079function vset_validate() 1080 1081{ 1082 1083 var bRet = true; 1084 1085 for (var itr = 0; itr < this.vSet.length; itr++) 1086 1087 { 1088 1089 bRet = bRet && this.vSet[itr].validate(); 1090 1091 if (!bRet && !this.msgs_together) 1092 1093 { 1094 1095 break; 1096 1097 } 1098 1099 } 1100 1101 return bRet; 1102 1103} 1104 1105 1106 1107/* checks the validity of an email address entered 1108 1109* returns true or false 1110 1111*/ 1112 1113function validateEmail(email) 1114 1115{ 1116 1117 var splitted = email.match("^(.+)@(.+)$"); 1118 1119 if (splitted == null) return false; 1120 1121 if (splitted[1] != null) 1122 1123 { 1124 1125 var regexp_user = /^\"?[\w-_\.]*\"?$/; 1126 1127 if (splitted[1].match(regexp_user) == null) return false; 1128 1129 } 1130 1131 if (splitted[2] != null) 1132 1133 { 1134 1135 var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/; 1136 1137 if (splitted[2].match(regexp_domain) == null) 1138 1139 { 1140 1141 var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/; 1142 1143 if (splitted[2].match(regexp_ip) == null) return false; 1144 1145 } // if 1146 1147 return true; 1148 1149 } 1150 1151 return false; 1152 1153} 1154 1155 1156 1157function TestComparison(objValue, strCompareElement, strvalidator, strError) 1158 1159{ 1160 1161 var bRet = true; 1162 1163 var objCompare = null; 1164 1165 if (!objValue.form) 1166 1167 { 1168 1169 sfm_show_error_msg("Error: No Form object!", objValue); 1170 1171 return false 1172 1173 } 1174 1175 objCompare = objValue.form.elements[strCompareElement]; 1176 1177 if (!objCompare) 1178 1179 { 1180 1181 sfm_show_error_msg("Error: Element with name" + strCompareElement + " not found !", objValue); 1182 1183 return false; 1184 1185 } 1186 1187 1188 1189 var objval_value = objValue.value; 1190 1191 var objcomp_value = objCompare.value; 1192 1193 1194 1195 if (strvalidator != "eqelmnt" && strvalidator != "neelmnt") 1196 1197 { 1198 1199 objval_value = objval_value.replace(/\,/g, ""); 1200 1201 objcomp_value = objcomp_value.replace(/\,/g, ""); 1202 1203 1204 1205 if (isNaN(objval_value)) 1206 1207 { 1208 1209 sfm_show_error_msg(objValue.name + ": Should be a number ", objValue); 1210 1211 return false; 1212 1213 } //if 1214 1215 if (isNaN(objcomp_value)) 1216 1217 { 1218 1219 sfm_show_error_msg(objCompare.name + ": Should be a number ", objCompare); 1220 1221 return false; 1222 1223 } //if 1224 1225 } //if 1226 1227 var cmpstr = ""; 1228 1229 switch (strvalidator) 1230 1231 { 1232 1233 case "eqelmnt": 1234 1235 { 1236 1237 if (objval_value != objcomp_value) 1238 1239 { 1240 1241 cmpstr = " should be equal to "; 1242 1243 bRet = false; 1244 1245 } //if 1246 1247 break; 1248 1249 } //case 1250 1251 case "ltelmnt": 1252 1253 { 1254 1255 if (eval(objval_value) >= eval(objcomp_value)) 1256 1257 { 1258 1259 cmpstr = " should be less than "; 1260 1261 bRet = false; 1262 1263 } 1264 1265 break; 1266 1267 } //case 1268 1269 case "leelmnt": 1270 1271 { 1272 1273 if (eval(objval_value) > eval(objcomp_value)) 1274 1275 { 1276 1277 cmpstr = " should be less than or equal to"; 1278 1279 bRet = false; 1280 1281 } 1282 1283 break; 1284 1285 } //case 1286 1287 case "gtelmnt": 1288 1289 { 1290 1291 if (eval(objval_value) <= eval(objcomp_value)) 1292 1293 { 1294 1295 cmpstr = " should be greater than"; 1296 1297 bRet = false; 1298 1299 } 1300 1301 break; 1302 1303 } //case 1304 1305 case "geelmnt": 1306 1307 { 1308 1309 if (eval(objval_value) < eval(objcomp_value)) 1310 1311 { 1312 1313 cmpstr = " should be greater than or equal to"; 1314 1315 bRet = false; 1316 1317 } 1318 1319 break; 1320 1321 } //case 1322 1323 case "neelmnt": 1324 1325 { 1326 1327 if (objval_value.length > 0 && objcomp_value.length > 0 && objval_value == objcomp_value) 1328 1329 { 1330 1331 cmpstr = " should be different from "; 1332 1333 bRet = false; 1334 1335 } //if 1336 1337 break; 1338 1339 } 1340 1341 } //switch 1342 1343 if (bRet == false) 1344 1345 { 1346 1347 if (!strError || strError.length == 0) 1348 1349 { 1350 1351 strError = objValue.name + cmpstr + objCompare.name; 1352 1353 } //if 1354 1355 sfm_show_error_msg(strError, objValue); 1356 1357 } //if 1358 1359 return bRet; 1360 1361} 1362 1363 1364 1365function TestSelMin(objValue, strMinSel, strError) 1366 1367{ 1368 1369 var bret = true; 1370 1371 var objcheck = objValue.form.elements[objValue.name]; 1372 1373 var chkcount = 0; 1374 1375 if (objcheck.length) 1376 1377 { 1378 1379 for (var c = 0; c < objcheck.length; c++) 1380 1381 { 1382 1383 if (objcheck[c].checked == "1") 1384 1385 { 1386 1387 chkcount++; 1388 1389 } //if 1390 1391 } //for 1392 1393 } 1394 1395 else 1396 1397 { 1398 1399 chkcount = (objcheck.checked == "1") ? 1 : 0; 1400 1401 } 1402 1403 var minsel = eval(strMinSel); 1404 1405 if (chkcount < minsel) 1406 1407 { 1408 1409 if (!strError || strError.length == 0) 1410 1411 { 1412 1413 strError = "Please Select atleast" + minsel + " check boxes for" + objValue.name; 1414 1415 } //if 1416 1417 sfm_show_error_msg(strError, objValue); 1418 1419 bret = false; 1420 1421 } 1422 1423 return bret; 1424 1425} 1426 1427 1428 1429function TestSelMax(objValue, strMaxSel, strError) 1430 1431{ 1432 1433 var bret = true; 1434 1435 var objcheck = objValue.form.elements[objValue.name]; 1436 1437 var chkcount = 0; 1438 1439 if (objcheck.length) 1440 1441 { 1442 1443 for (var c = 0; c < objcheck.length; c++) 1444 1445 { 1446 1447 if (objcheck[c].checked == "1") 1448 1449 { 1450 1451 chkcount++; 1452 1453 } //if 1454 1455 } //for 1456 1457 } 1458 1459 else 1460 1461 { 1462 1463 chkcount = (objcheck.checked == "1") ? 1 : 0; 1464 1465 } 1466 1467 var maxsel = eval(strMaxSel); 1468 1469 if (chkcount > maxsel) 1470 1471 { 1472 1473 if (!strError || strError.length == 0) 1474 1475 { 1476 1477 strError = "Please Select atmost " + maxsel + " check boxes for" + objValue.name; 1478 1479 } //if 1480 1481 sfm_show_error_msg(strError, objValue); 1482 1483 bret = false; 1484 1485 } 1486 1487 return bret; 1488 1489} 1490 1491 1492 1493function IsCheckSelected(objValue, chkValue) 1494 1495{ 1496 1497 var selected = false; 1498 1499 var objcheck = objValue.form.elements[objValue.name]; 1500 1501 if (objcheck.length) 1502 1503 { 1504 1505 var idxchk = -1; 1506 1507 for (var c = 0; c < objcheck.length; c++) 1508 1509 { 1510 1511 if (objcheck[c].value == chkValue) 1512 1513 { 1514 1515 idxchk = c; 1516 1517 break; 1518 1519 } //if 1520 1521 } //for 1522 1523 1524 if (idxchk >= 0) 1525 1526 { 1527 1528 if (objcheck[idxchk].checked == "1") 1529 1530 { 1531 1532 selected = true; 1533 1534 } 1535 1536 } //if 1537 1538 } 1539 1540 else 1541 1542 { 1543 1544 if (objValue.checked == "1") 1545 1546 { 1547 1548 selected = true; 1549 1550 } //if 1551 1552 } //else 1553 1554 return selected; 1555 1556} 1557 1558 1559 1560function TestDontSelectChk(objValue, chkValue, strError) 1561 1562{ 1563 1564 var pass = true; 1565 1566 pass = IsCheckSelected(objValue, chkValue) ? false : true; 1567 1568 1569 1570 if (pass == false) 1571 1572 { 1573 1574 if (!strError || strError.length == 0) 1575 1576 { 1577 1578 strError = "Can't Proceed as you selected " + objValue.name; 1579 1580 } //if 1581 1582 sfm_show_error_msg(strError, objValue); 1583 1584 1585 1586 } 1587 1588 return pass; 1589 1590} 1591 1592 1593 1594function TestShouldSelectChk(objValue, chkValue, strError) 1595 1596{ 1597 1598 var pass = true; 1599 1600 1601 1602 pass = IsCheckSelected(objValue, chkValue) ? true : false; 1603 1604 1605 1606 if (pass == false) 1607 1608 { 1609 1610 if (!strError || strError.length == 0) 1611 1612 { 1613 1614 strError = "You should select" + objValue.name; 1615 1616 } //if 1617 1618 sfm_show_error_msg(strError, objValue); 1619 1620 1621 1622 } 1623 1624 return pass; 1625 1626} 1627 1628 1629 1630function TestRequiredInput(objValue, strError) 1631 1632{ 1633 1634 var ret = true; 1635 1636 if (VWZ_IsEmpty(objValue.value)) 1637 1638 { 1639 1640 ret = false; 1641 1642 } //if 1643
1644 else if (objValue.getcal && !objValue.getcal()) 1645 1646 { 1647 1648 ret = false; 1649 1650 } 1651 1652 1653 1654 if (!ret) 1655 1656 { 1657 1658 if (!strError || strError.length == 0) 1659 1660 { 1661 1662 strError = objValue.name + " : Required Field"; 1663 1664 } //if 1665 1666 sfm_show_error_msg(strError, objValue); 1667 1668 } 1669 1670 return ret; 1671 1672} 1673 1674 1675 1676function TestFileExtension(objValue, cmdvalue, strError) 1677 1678{ 1679 1680 var ret = false; 1681 1682 var found = false; 1683 1684 1685 1686 if (objValue.value.length <= 0) 1687 1688 { //The 'required' validation is not done here 1689 1690 return true; 1691 1692 } 1693 1694 1695 1696 var extns = cmdvalue.split(";"); 1697 1698 for (var i = 0; i < extns.length; i++) 1699 1700 { 1701 1702 ext = objValue.value.substr(objValue.value.length - extns[i].length, extns[i].length); 1703 1704 ext = ext.toLowerCase(); 1705 1706 if (ext == extns[i]) 1707 1708 { 1709 1710 found = true; 1711 1712 break; 1713 1714 } 1715 1716 } 1717 1718 if (!found) 1719 1720 { 1721 1722 if (!strError || strError.length == 0) 1723 1724 { 1725 1726 strError = objValue.name + " allowed file extensions are: " + cmdvalue; 1727 1728 } //if 1729 1730 sfm_show_error_msg(strError, objValue); 1731 1732 ret = false; 1733 1734 } 1735 1736 else 1737 1738 { 1739 1740 ret = true; 1741 1742 } 1743 1744 return ret; 1745 1746} 1747 1748 1749 1750function TestMaxLen(objValue, strMaxLen, strError) 1751 1752{ 1753 1754 var ret = true; 1755 1756 if (eval(objValue.value.length) > eval(strMaxLen)) 1757 1758 { 1759 1760 if (!strError || strError.length == 0) 1761 1762 { 1763 1764 strError = objValue.name + " : " + strMaxLen + " characters maximum "; 1765 1766 } //if 1767 1768 sfm_show_error_msg(strError, objValue); 1769 1770 ret = false; 1771 1772 } //if 1773 1774 return ret; 1775 1776} 1777 1778 1779 1780function TestMinLen(objValue, strMinLen, strError) 1781 1782{ 1783 1784 var ret = true; 1785 1786 if (eval(objValue.value.length) < eval(strMinLen)) 1787 1788 { 1789 1790 if (!strError || strError.length == 0) 1791 1792 { 1793 1794 strError = objValue.name + " : " + strMinLen + " characters minimum "; 1795 1796 } //if 1797 1798 sfm_show_error_msg(strError, objValue); 1799 1800 ret = false; 1801 1802 } //if 1803 1804 return ret; 1805 1806} 1807 1808 1809 1810function TestInputType(objValue, strRegExp, strError, strDefaultError) 1811 1812{ 1813 1814 var ret = true; 1815 1816 1817 1818 var charpos = objValue.value.search(strRegExp); 1819 1820 if (objValue.value.length > 0 && charpos >= 0) 1821 1822 { 1823 1824 if (!strError || strError.length == 0) 1825 1826 { 1827 1828 strError = strDefaultError; 1829 1830 } //if 1831 1832 sfm_show_error_msg(strError, objValue); 1833 1834 ret = false; 1835 1836 } //if 1837 1838 return ret; 1839 1840} 1841 1842 1843 1844function TestEmail(objValue, strError) 1845 1846{ 1847 1848 var ret = true; 1849 1850 if (objValue.value.length > 0 && !validateEmail(objValue.value)) 1851 1852 { 1853 1854 if (!strError || strError.length == 0) 1855 1856 { 1857 1858 strError = objValue.name + ": Enter a valid Email address "; 1859 1860 } //if 1861 1862 sfm_show_error_msg(strError, objValue); 1863 1864 ret = false; 1865 1866 } //if 1867 1868 return ret; 1869 1870} 1871 1872 1873 1874function TestLessThan(objValue, strLessThan, strError) 1875 1876{ 1877 1878 var ret = true; 1879 1880 var obj_value = objValue.value.replace(/\,/g, ""); 1881 1882 strLessThan = strLessThan.replace(/\,/g, ""); 1883 1884 1885 1886 if (isNaN(obj_value)) 1887 1888 { 1889 1890 sfm_show_error_msg(objValue.name + ": Should be a number ", objValue); 1891 1892 ret = false; 1893 1894 } //if 1895 1896 else if (eval(obj_value) >= eval(strLessThan)) 1897 1898 { 1899 1900 if (!strError || strError.length == 0) 1901 1902 { 1903 1904 strError = objValue.name + " : value should be less than " + strLessThan; 1905 1906 } //if 1907 1908 sfm_show_error_msg(strError, objValue); 1909 1910 ret = false; 1911 1912 } //if 1913 1914 return ret; 1915 1916} 1917 1918 1919 1920function TestGreaterThan(objValue, strGreaterThan, strError) 1921 1922{ 1923 1924 var ret = true; 1925 1926 var obj_value = objValue.value.replace(/\,/g, ""); 1927 1928 strGreaterThan = strGreaterThan.replace(/\,/g, ""); 1929 1930 1931 1932 if (isNaN(obj_value)) 1933 1934 { 1935 1936 sfm_show_error_msg(objValue.name + ": Should be a number ", objValue); 1937 1938 ret = false; 1939 1940 } //if 1941 1942 else if (eval(obj_value) <= eval(strGreaterThan)) 1943 1944 { 1945 1946 if (!strError || strError.length == 0) 1947 1948 { 1949 1950 strError = objValue.name + " : value should be greater than " + strGreaterThan; 1951 1952 } //if 1953 1954 sfm_show_error_msg(strError, objValue); 1955 1956 ret = false; 1957 1958 } //if 1959 1960 return ret; 1961 1962} 1963 1964 1965 1966function TestRegExp(objValue, strRegExp, strError) 1967 1968{ 1969 1970 var ret = true; 1971 1972 if (objValue.value.length > 0 && !objValue.value.match(strRegExp)) 1973 1974 { 1975 1976 if (!strError || strError.length == 0) 1977 1978 { 1979 1980 strError = objValue.name + ": Invalid characters found "; 1981 1982 } //if 1983 1984 sfm_show_error_msg(strError, objValue); 1985 1986 ret = false; 1987 1988 } //if 1989 1990 return ret; 1991 1992} 1993 1994 1995 1996function TestDontSelect(objValue, dont_sel_value, strError) 1997 1998{ 1999 2000 var ret = true; 2001 2002 if (objValue.value == null) 2003 2004 { 2005 2006 sfm_show_error_msg("Error: dontselect command for non-select Item", objValue); 2007 2008 ret = false; 2009 2010 } 2011 2012 else if (objValue.value == dont_sel_value) 2013 2014 { 2015 2016 if (!strError || strError.length == 0) 2017 2018 { 2019 2020 strError = objValue.name + ": Please Select one option "; 2021 2022 } //if 2023 2024 sfm_show_error_msg(strError, objValue); 2025 2026 ret = false; 2027 2028 } 2029 2030 return ret; 2031 2032} 2033 2034 2035 2036function TestSelectOneRadio(objValue, strError) 2037 2038{ 2039 2040 var objradio = objValue.form.elements[objValue.name]; 2041 2042 var one_selected = false; 2043 2044 for (var r = 0; r < objradio.length; r++) 2045 2046 { 2047 2048 if (objradio[r].checked == "1") 2049 2050 { 2051 2052 one_selected = true; 2053 2054 break; 2055 2056 } 2057 2058 } 2059 2060 if (false == one_selected) 2061 2062 { 2063 2064 if (!strError || strError.length == 0) 2065 2066 { 2067 2068 strError = "Please select one option from " + objValue.name; 2069 2070 } 2071 2072 sfm_show_error_msg(strError, objValue); 2073 2074 } 2075 2076 return one_selected; 2077 2078} 2079 2080 2081 2082function TestSelectRadio(objValue, cmdvalue, strError, testselect) 2083 2084{ 2085 2086 var objradio = objValue.form.elements[objValue.name]; 2087 2088 var selected = false; 2089 2090 2091 2092 for (var r = 0; r < objradio.length; r++) 2093 2094 { 2095 2096 if (objradio[r].value == cmdvalue && objradio[r].checked == "1") 2097 2098 { 2099 2100 selected = true; 2101 2102 break; 2103 2104 } 2105 2106 } 2107 2108 if (testselect == true && false == selected || testselect == false && true == selected) 2109 2110 { 2111 2112 sfm_show_error_msg(strError, objValue); 2113 2114 return false; 2115 2116 } 2117 2118 return true; 2119 2120} 2121 2122 2123 2124 2125 2126//* Checks each field in a form 2127 2128 2129 2130 2131 2132function validateInput(strValidateStr, objValue, strError) 2133 2134{ 2135 2136 2137 2138 var ret = true; 2139 2140 var epos = strValidateStr.search("="); 2141 2142 var command = ""; 2143 2144 var cmdvalue = ""; 2145 2146 if (epos >= 0) 2147 2148 { 2149 2150 command = strValidateStr.substring(0, epos); 2151 2152 cmdvalue = strValidateStr.substr(epos + 1); 2153 2154 } 2155 2156 else 2157 2158 { 2159 2160 command = strValidateStr; 2161 2162 } 2163 2164 2165 2166 switch (command) 2167 2168 { 2169 2170 case "req": 2171 2172 case "required": 2173 2174 { 2175 2176 ret = TestRequiredInput(objValue, strError) 2177 2178 break; 2179 2180 } 2181 2182 case "maxlength": 2183 2184 case "maxlen": 2185 2186 { 2187 2188 ret = TestMaxLen(objValue, cmdvalue, strError) 2189 2190 break; 2191 2192 } 2193 2194 case "minlength": 2195 2196 case "minlen": 2197 2198 { 2199 2200 ret = TestMinLen(objValue, cmdvalue, strError) 2201 2202 break; 2203 2204 } 2205 2206 case "alnum": 2207 2208 case "alphanumeric": 2209 2210 { 2211 2212 ret = TestInputType(objValue, "[^A-Za-z0-9]", strError, objValue.name + ": Only alpha-numeric characters allowed "); 2213 2214 break; 2215 2216 } 2217 2218 case "alnum_s": 2219 2220 case "alphanumeric_space": 2221 2222 { 2223 2224 ret = TestInputType(objValue, "[^A-Za-z0-9\\s]", strError, objValue.name + ": Only alpha-numeric characters and space allowed "); 2225 2226 break; 2227 2228 } 2229 2230 case "num": 2231 2232 case "numeric": 2233 2234 case "dec": 2235 2236 case "decimal": 2237 2238 { 2239 2240 if (objValue.value.length > 0 && !objValue.value.match(/^[\-\+]?[\d\,]*\.?[\d]*$/)) 2241 2242 { 2243 2244 sfm_show_error_msg(strError, objValue); 2245 2246 ret = false; 2247 2248 } //if 2249 2250 break; 2251 2252 } 2253 2254 case "alphabetic": 2255 2256 case "alpha": 2257 2258 { 2259 2260 ret = TestInputType(objValue, "[^A-Za-z]", strError, objValue.name + ": Only alphabetic characters allowed "); 2261 2262 break; 2263 2264 } 2265 2266 case "alphabetic_space": 2267 2268 case "alpha_s": 2269 2270 { 2271 2272 ret = TestInputType(objValue, "[^A-Za-z\\s]", strError, objValue.name + ": Only alphabetic characters and space allowed "); 2273 2274 break; 2275 2276 } 2277 2278 case "email": 2279 2280 { 2281 2282 ret = TestEmail(objValue, strError); 2283 2284 break; 2285 2286 } 2287 2288 case "lt": 2289 2290 case "lessthan": 2291 2292 { 2293 2294 ret = TestLessThan(objValue, cmdvalue, strError); 2295 2296 break; 2297 2298 } 2299 2300 case "gt": 2301 2302 case "greaterthan": 2303 2304 { 2305 2306 ret = TestGreaterThan(objValue, cmdvalue, strError); 2307 2308 break; 2309 2310 } 2311 2312 case "regexp": 2313 2314 { 2315 2316 ret = TestRegExp(objValue, cmdvalue, strError); 2317 2318 break; 2319 2320 } 2321 2322 case "dontselect": 2323 2324 { 2325 2326 ret = TestDontSelect(objValue, cmdvalue, strError) 2327 2328 break; 2329 2330 } 2331 2332 case "dontselectchk": 2333 2334 { 2335 2336 ret = TestDontSelectChk(objValue, cmdvalue, strError) 2337 2338 break; 2339 2340 } 2341 2342 case "shouldselchk": 2343 2344 { 2345 2346 ret = TestShouldSelectChk(objValue, cmdvalue, strError) 2347 2348 break; 2349 2350 } 2351 2352 case "selmin": 2353 2354 { 2355 2356 ret = TestSelMin(objValue, cmdvalue, strError); 2357 2358 break; 2359 2360 } 2361 2362 case "selmax": 2363 2364 { 2365 2366 ret = TestSelMax(objValue, cmdvalue, strError); 2367 2368 break; 2369 2370 } 2371 2372 case "selone_radio": 2373 2374 case "selone": 2375 2376 { 2377 2378 ret = TestSelectOneRadio(objValue, strError); 2379 2380 break; 2381 2382 } 2383 2384 case "dontselectradio": 2385 2386 { 2387 2388 ret = TestSelectRadio(objValue, cmdvalue, strError, false); 2389 2390 break; 2391 2392 } 2393 2394 case "selectradio": 2395 2396 { 2397 2398 ret = TestSelectRadio(objValue, cmdvalue, strError, true); 2399 2400 break; 2401 2402 } 2403 2404 //Comparisons 2405 2406 case "eqelmnt": 2407 2408 case "ltelmnt": 2409 2410 case "leelmnt": 2411 2412 case "gtelmnt": 2413 2414 case "geelmnt": 2415 2416 case "neelmnt": 2417 2418 { 2419 2420 return TestComparison(objValue, cmdvalue, command, strError); 2421 2422 break; 2423 2424 } 2425 2426 case "req_file": 2427 2428 { 2429 2430 ret = TestRequiredInput(objValue, strError); 2431 2432 break; 2433 2434 } 2435 2436 case "file_extn": 2437 2438 { 2439 2440 ret = TestFileExtension(objValue, cmdvalue, strError); 2441 2442 break; 2443 2444 } 2445 2446 2447 2448 } //switch 2449 2450 return ret; 2451 2452} 2453 2454 2455 2456function VWZ_IsListItemSelected(listname, value) 2457 2458{ 2459 2460 for (var i = 0; i < listname.options.length; i++) 2461 2462 { 2463 2464 if (listname.options[i].selected == true && listname.options[i].value == value) 2465 2466 { 2467 2468 return true; 2469 2470 } 2471 2472 } 2473 2474 return false; 2475 2476} 2477 2478 2479 2480function VWZ_IsChecked(objcheck, value) 2481 2482{ 2483 2484 if (objcheck.length) 2485 2486 { 2487 2488 for (var c = 0; c < objcheck.length; c++) 2489 2490 { 2491 2492 if (objcheck[c].checked == "1" && objcheck[c].value == value) 2493 2494 { 2495 2496 return true; 2497 2498 } 2499 2500 } 2501 2502 } 2503 2504 else 2505 2506 { 2507 2508 if (objcheck.checked == "1") 2509 2510 { 2511 2512 return true; 2513 2514 } 2515 2516 } 2517 2518 return false; 2519 2520} 2521 2522 2523 2524function sfm_str_trim(strIn) 2525 2526{ 2527 2528 return strIn.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 2529 2530} 2531 2532 2533 2534function VWZ_IsEmpty(value) 2535 2536{ 2537 2538 value = sfm_str_trim(value); 2539 2540 return (value.length) == 0 ? true : false; 2541 2542} 2543 2544/* 2545 2546 Copyright (C) 2003-2011 JavaScript-Coder.com . All rights reserved. 2547
2548*/
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.