Advertisement
Guest User

MS Validators

a guest
Aug 5th, 2011
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.11 KB | None | 0 0
  1. var Page_ValidationVer = "125";
  2. var Page_IsValid = true;
  3. var Page_BlockSubmit = false;
  4. var Page_InvalidControlToBeFocused = null;
  5. function ValidatorUpdateDisplay(val) {
  6. if (typeof(val.display) == "string") {
  7. if (val.display == "None") {
  8. return;
  9. }
  10. if (val.display == "Dynamic") {
  11. val.style.display = val.isvalid ? "none" : "inline";
  12. return;
  13. }
  14. }
  15. if ((navigator.userAgent.indexOf("Mac") > -1) &&
  16. (navigator.userAgent.indexOf("MSIE") > -1)) {
  17. val.style.display = "inline";
  18. }
  19. val.style.visibility = val.isvalid ? "hidden" : "visible";
  20. }
  21. function ValidatorUpdateIsValid() {
  22. Page_IsValid = AllValidatorsValid(Page_Validators);
  23. }
  24. function AllValidatorsValid(validators) {
  25. if ((typeof(validators) != "undefined") && (validators != null)) {
  26. var i;
  27. for (i = 0; i < validators.length; i++) {
  28. if (!validators[i].isvalid) {
  29. return false;
  30. }
  31. }
  32. }
  33. return true;
  34. }
  35. function ValidatorHookupControlID(controlID, val) {
  36. if (typeof(controlID) != "string") {
  37. return;
  38. }
  39. var ctrl = document.getElementById(controlID);
  40. if ((typeof(ctrl) != "undefined") && (ctrl != null)) {
  41. ValidatorHookupControl(ctrl, val);
  42. }
  43. else {
  44. val.isvalid = true;
  45. val.enabled = false;
  46. }
  47. }
  48. function ValidatorHookupControl(control, val) {
  49. if (typeof(control.tagName) != "string") {
  50. return;
  51. }
  52. if (control.tagName != "INPUT" && control.tagName != "TEXTAREA" && control.tagName != "SELECT") {
  53. var i;
  54. for (i = 0; i < control.childNodes.length; i++) {
  55. ValidatorHookupControl(control.childNodes[i], val);
  56. }
  57. return;
  58. }
  59. else {
  60. if (typeof(control.Validators) == "undefined") {
  61. control.Validators = new Array;
  62. var eventType;
  63. if (control.type == "radio") {
  64. eventType = "onclick";
  65. } else {
  66. eventType = "onchange";
  67. if (typeof(val.focusOnError) == "string" && val.focusOnError == "t") {
  68. ValidatorHookupEvent(control, "onblur", "ValidatedControlOnBlur(event); ");
  69. }
  70. }
  71. ValidatorHookupEvent(control, eventType, "ValidatorOnChange(event); ");
  72. if (control.type == "text" ||
  73. control.type == "password" ||
  74. control.type == "file") {
  75. ValidatorHookupEvent(control, "onkeypress",
  76. "if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); return false; } ");
  77. }
  78. }
  79. control.Validators[control.Validators.length] = val;
  80. }
  81. }
  82. function ValidatorHookupEvent(control, eventType, functionPrefix) {
  83. var ev;
  84. eval("ev = control." + eventType + ";");
  85. if (typeof(ev) == "function") {
  86. ev = ev.toString();
  87. ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
  88. }
  89. else {
  90. ev = "";
  91. }
  92. var func;
  93. if (navigator.appName.toLowerCase().indexOf('explorer') > -1) {
  94. func = new Function(functionPrefix + " " + ev);
  95. }
  96. else {
  97. func = new Function("event", functionPrefix + " " + ev);
  98. }
  99. eval("control." + eventType + " = func;");
  100. }
  101. function ValidatorGetValue(id) {
  102. var control;
  103. control = document.getElementById(id);
  104. if (typeof(control.value) == "string") {
  105. return control.value;
  106. }
  107. return ValidatorGetValueRecursive(control);
  108. }
  109. function ValidatorGetValueRecursive(control)
  110. {
  111. if (typeof(control.value) == "string" && (control.type != "radio" || control.checked == true)) {
  112. return control.value;
  113. }
  114. var i, val;
  115. for (i = 0; i<control.childNodes.length; i++) {
  116. val = ValidatorGetValueRecursive(control.childNodes[i]);
  117. if (val != "") return val;
  118. }
  119. return "";
  120. }
  121. function Page_ClientValidate(validationGroup) {
  122. Page_InvalidControlToBeFocused = null;
  123. if (typeof(Page_Validators) == "undefined") {
  124. return true;
  125. }
  126. var i;
  127. for (i = 0; i < Page_Validators.length; i++) {
  128. ValidatorValidate(Page_Validators[i], validationGroup, null);
  129. }
  130. ValidatorUpdateIsValid();
  131. ValidationSummaryOnSubmit(validationGroup);
  132. Page_BlockSubmit = !Page_IsValid;
  133. return Page_IsValid;
  134. }
  135. function ValidatorCommonOnSubmit() {
  136. Page_InvalidControlToBeFocused = null;
  137. var result = !Page_BlockSubmit;
  138. if ((typeof(window.event) != "undefined") && (window.event != null)) {
  139. window.event.returnValue = result;
  140. }
  141. Page_BlockSubmit = false;
  142. return result;
  143. }
  144. function ValidatorEnable(val, enable) {
  145. val.enabled = (enable != false);
  146. ValidatorValidate(val);
  147. ValidatorUpdateIsValid();
  148. }
  149. function ValidatorOnChange(event) {
  150. if (!event) {
  151. event = window.event;
  152. }
  153. Page_InvalidControlToBeFocused = null;
  154. var targetedControl;
  155. if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null)) {
  156. targetedControl = event.srcElement;
  157. }
  158. else {
  159. targetedControl = event.target;
  160. }
  161. var vals;
  162. if (typeof(targetedControl.Validators) != "undefined") {
  163. vals = targetedControl.Validators;
  164. }
  165. else {
  166. if (targetedControl.tagName.toLowerCase() == "label") {
  167. targetedControl = document.getElementById(targetedControl.htmlFor);
  168. vals = targetedControl.Validators;
  169. }
  170. }
  171. var i;
  172. for (i = 0; i < vals.length; i++) {
  173. ValidatorValidate(vals[i], null, event);
  174. }
  175. ValidatorUpdateIsValid();
  176. }
  177. function ValidatedTextBoxOnKeyPress(event) {
  178. if (event.keyCode == 13) {
  179. ValidatorOnChange(event);
  180. var vals;
  181. if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null)) {
  182. vals = event.srcElement.Validators;
  183. }
  184. else {
  185. vals = event.target.Validators;
  186. }
  187. return AllValidatorsValid(vals);
  188. }
  189. return true;
  190. }
  191. function ValidatedControlOnBlur(event) {
  192. var control;
  193. if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null)) {
  194. control = event.srcElement;
  195. }
  196. else {
  197. control = event.target;
  198. }
  199. if ((typeof(control) != "undefined") && (control != null) && (Page_InvalidControlToBeFocused == control)) {
  200. control.focus();
  201. Page_InvalidControlToBeFocused = null;
  202. }
  203. }
  204. function ValidatorValidate(val, validationGroup, event) {
  205. val.isvalid = true;
  206. if ((typeof(val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
  207. if (typeof(val.evaluationfunction) == "function") {
  208. val.isvalid = val.evaluationfunction(val);
  209. if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
  210. typeof(val.focusOnError) == "string" && val.focusOnError == "t") {
  211. ValidatorSetFocus(val, event);
  212. }
  213. }
  214. }
  215. ValidatorUpdateDisplay(val);
  216. }
  217. function ValidatorSetFocus(val, event) {
  218. var ctrl;
  219. if (typeof(val.controlhookup) == "string") {
  220. var eventCtrl;
  221. if ((typeof(event) != "undefined") && (event != null)) {
  222. if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null)) {
  223. eventCtrl = event.srcElement;
  224. }
  225. else {
  226. eventCtrl = event.target;
  227. }
  228. }
  229. if ((typeof(eventCtrl) != "undefined") && (eventCtrl != null) &&
  230. (typeof(eventCtrl.id) == "string") &&
  231. (eventCtrl.id == val.controlhookup)) {
  232. ctrl = eventCtrl;
  233. }
  234. }
  235. if ((typeof(ctrl) == "undefined") || (ctrl == null)) {
  236. ctrl = document.getElementById(val.controltovalidate);
  237. }
  238. if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
  239. (ctrl.tagName.toLowerCase() != "table" || (typeof(event) == "undefined") || (event == null)) &&
  240. ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
  241. (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
  242. (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
  243. (IsInVisibleContainer(ctrl))) {
  244. if ((ctrl.tagName.toLowerCase() == "table" && (typeof(__nonMSDOMBrowser) == "undefined" || __nonMSDOMBrowser)) ||
  245. (ctrl.tagName.toLowerCase() == "span")) {
  246. var inputElements = ctrl.getElementsByTagName("input");
  247. var lastInputElement = inputElements[inputElements.length -1];
  248. if (lastInputElement != null) {
  249. ctrl = lastInputElement;
  250. }
  251. }
  252. if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
  253. ctrl.focus();
  254. Page_InvalidControlToBeFocused = ctrl;
  255. }
  256. }
  257. }
  258. function IsInVisibleContainer(ctrl) {
  259. if (typeof(ctrl.style) != "undefined" &&
  260. ( ( typeof(ctrl.style.display) != "undefined" &&
  261. ctrl.style.display == "none") ||
  262. ( typeof(ctrl.style.visibility) != "undefined" &&
  263. ctrl.style.visibility == "hidden") ) ) {
  264. return false;
  265. }
  266. else if (typeof(ctrl.parentNode) != "undefined" &&
  267. ctrl.parentNode != null &&
  268. ctrl.parentNode != ctrl) {
  269. return IsInVisibleContainer(ctrl.parentNode);
  270. }
  271. return true;
  272. }
  273. function IsValidationGroupMatch(control, validationGroup) {
  274. if ((typeof(validationGroup) == "undefined") || (validationGroup == null)) {
  275. return true;
  276. }
  277. var controlGroup = "";
  278. if (typeof(control.validationGroup) == "string") {
  279. controlGroup = control.validationGroup;
  280. }
  281. return (controlGroup == validationGroup);
  282. }
  283. function ValidatorOnLoad() {
  284. if (typeof(Page_Validators) == "undefined")
  285. return;
  286. var i, val;
  287. for (i = 0; i < Page_Validators.length; i++) {
  288. val = Page_Validators[i];
  289. if (typeof(val.evaluationfunction) == "string") {
  290. eval("val.evaluationfunction = " + val.evaluationfunction + ";");
  291. }
  292. if (typeof(val.isvalid) == "string") {
  293. if (val.isvalid == "False") {
  294. val.isvalid = false;
  295. Page_IsValid = false;
  296. }
  297. else {
  298. val.isvalid = true;
  299. }
  300. } else {
  301. val.isvalid = true;
  302. }
  303. if (typeof(val.enabled) == "string") {
  304. val.enabled = (val.enabled != "False");
  305. }
  306. if (typeof(val.controltovalidate) == "string") {
  307. ValidatorHookupControlID(val.controltovalidate, val);
  308. }
  309. if (typeof(val.controlhookup) == "string") {
  310. ValidatorHookupControlID(val.controlhookup, val);
  311. }
  312. }
  313. Page_ValidationActive = true;
  314. }
  315. function ValidatorConvert(op, dataType, val) {
  316. function GetFullYear(year) {
  317. var twoDigitCutoffYear = val.cutoffyear % 100;
  318. var cutoffYearCentury = val.cutoffyear - twoDigitCutoffYear;
  319. return ((year > twoDigitCutoffYear) ? (cutoffYearCentury - 100 + year) : (cutoffYearCentury + year));
  320. }
  321. var num, cleanInput, m, exp;
  322. if (dataType == "Integer") {
  323. exp = /^\s*[-\+]?\d+\s*$/;
  324. if (op.match(exp) == null)
  325. return null;
  326. num = parseInt(op, 10);
  327. return (isNaN(num) ? null : num);
  328. }
  329. else if(dataType == "Double") {
  330. exp = new RegExp("^\\s*([-\\+])?(\\d*)\\" + val.decimalchar + "?(\\d*)\\s*$");
  331. m = op.match(exp);
  332. if (m == null)
  333. return null;
  334. if (m[2].length == 0 && m[3].length == 0)
  335. return null;
  336. cleanInput = (m[1] != null ? m[1] : "") + (m[2].length>0 ? m[2] : "0") + (m[3].length>0 ? "." + m[3] : "");
  337. num = parseFloat(cleanInput);
  338. return (isNaN(num) ? null : num);
  339. }
  340. else if (dataType == "Currency") {
  341. var hasDigits = (val.digits > 0);
  342. var beginGroupSize, subsequentGroupSize;
  343. var groupSizeNum = parseInt(val.groupsize, 10);
  344. if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
  345. beginGroupSize = "{1," + groupSizeNum + "}";
  346. subsequentGroupSize = "{" + groupSizeNum + "}";
  347. }
  348. else {
  349. beginGroupSize = subsequentGroupSize = "+";
  350. }
  351. exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + val.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
  352. + (hasDigits ? "\\" + val.decimalchar + "?(\\d{0," + val.digits + "})" : "")
  353. + "\\s*$");
  354. m = op.match(exp);
  355. if (m == null)
  356. return null;
  357. if (m[2].length == 0 && hasDigits && m[5].length == 0)
  358. return null;
  359. cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
  360. num = parseFloat(cleanInput);
  361. return (isNaN(num) ? null : num);
  362. }
  363. else if (dataType == "Date") {
  364. var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$");
  365. m = op.match(yearFirstExp);
  366. var day, month, year;
  367. if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
  368. day = m[6];
  369. month = m[5];
  370. year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
  371. }
  372. else {
  373. if (val.dateorder == "ymd"){
  374. return null;
  375. }
  376. var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.)?\\s*$");
  377. m = op.match(yearLastExp);
  378. if (m == null) {
  379. return null;
  380. }
  381. if (val.dateorder == "mdy") {
  382. day = m[3];
  383. month = m[1];
  384. }
  385. else {
  386. day = m[1];
  387. month = m[3];
  388. }
  389. year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
  390. }
  391. month -= 1;
  392. var date = new Date(year, month, day);
  393. if (year < 100) {
  394. date.setFullYear(year);
  395. }
  396. return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
  397. }
  398. else {
  399. return op.toString();
  400. }
  401. }
  402. function ValidatorCompare(operand1, operand2, operator, val) {
  403. var dataType = val.type;
  404. var op1, op2;
  405. if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
  406. return false;
  407. if (operator == "DataTypeCheck")
  408. return true;
  409. if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
  410. return true;
  411. switch (operator) {
  412. case "NotEqual":
  413. return (op1 != op2);
  414. case "GreaterThan":
  415. return (op1 > op2);
  416. case "GreaterThanEqual":
  417. return (op1 >= op2);
  418. case "LessThan":
  419. return (op1 < op2);
  420. case "LessThanEqual":
  421. return (op1 <= op2);
  422. default:
  423. return (op1 == op2);
  424. }
  425. }
  426. function CompareValidatorEvaluateIsValid(val) {
  427. var value = ValidatorGetValue(val.controltovalidate);
  428. if (ValidatorTrim(value).length == 0)
  429. return true;
  430. var compareTo = "";
  431. if ((typeof(val.controltocompare) != "string") ||
  432. (typeof(document.getElementById(val.controltocompare)) == "undefined") ||
  433. (null == document.getElementById(val.controltocompare))) {
  434. if (typeof(val.valuetocompare) == "string") {
  435. compareTo = val.valuetocompare;
  436. }
  437. }
  438. else {
  439. compareTo = ValidatorGetValue(val.controltocompare);
  440. }
  441. var operator = "Equal";
  442. if (typeof(val.operator) == "string") {
  443. operator = val.operator;
  444. }
  445. return ValidatorCompare(value, compareTo, operator, val);
  446. }
  447. function CustomValidatorEvaluateIsValid(val) {
  448. var value = "";
  449. if (typeof(val.controltovalidate) == "string") {
  450. value = ValidatorGetValue(val.controltovalidate);
  451. if ((ValidatorTrim(value).length == 0) &&
  452. ((typeof(val.validateemptytext) != "string") || (val.validateemptytext != "true"))) {
  453. return true;
  454. }
  455. }
  456. var args = { Value:value, IsValid:true };
  457. if (typeof(val.clientvalidationfunction) == "string") {
  458. eval(val.clientvalidationfunction + "(val, args) ;");
  459. }
  460. return args.IsValid;
  461. }
  462. function RegularExpressionValidatorEvaluateIsValid(val) {
  463. var value = ValidatorGetValue(val.controltovalidate);
  464. if (ValidatorTrim(value).length == 0)
  465. return true;
  466. var rx = new RegExp(val.validationexpression);
  467. var matches = rx.exec(value);
  468. return (matches != null && value == matches[0]);
  469. }
  470. function ValidatorTrim(s) {
  471. var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
  472. return (m == null) ? "" : m[1];
  473. }
  474. function RequiredFieldValidatorEvaluateIsValid(val) {
  475. return (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))
  476. }
  477. function RangeValidatorEvaluateIsValid(val) {
  478. var value = ValidatorGetValue(val.controltovalidate);
  479. if (ValidatorTrim(value).length == 0)
  480. return true;
  481. return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
  482. ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));
  483. }
  484. function ValidationSummaryOnSubmit(validationGroup) {
  485. if (typeof(Page_ValidationSummaries) == "undefined")
  486. return;
  487. var summary, sums, s;
  488. for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
  489. summary = Page_ValidationSummaries[sums];
  490. summary.style.display = "none";
  491. if (!Page_IsValid && IsValidationGroupMatch(summary, validationGroup)) {
  492. var i;
  493. if (summary.showsummary != "False") {
  494. summary.style.display = "";
  495. if (typeof(summary.displaymode) != "string") {
  496. summary.displaymode = "BulletList";
  497. }
  498. switch (summary.displaymode) {
  499. case "List":
  500. headerSep = "<br>";
  501. first = "";
  502. pre = "";
  503. post = "<br>";
  504. end = "";
  505. break;
  506. case "BulletList":
  507. default:
  508. headerSep = "";
  509. first = "<ul>";
  510. pre = "<li>";
  511. post = "</li>";
  512. end = "</ul>";
  513. break;
  514. case "SingleParagraph":
  515. headerSep = " ";
  516. first = "";
  517. pre = "";
  518. post = " ";
  519. end = "<br>";
  520. break;
  521. }
  522. s = "";
  523. if (typeof(summary.headertext) == "string") {
  524. s += summary.headertext + headerSep;
  525. }
  526. s += first;
  527. for (i=0; i<Page_Validators.length; i++) {
  528. if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
  529. s += pre + Page_Validators[i].errormessage + post;
  530. }
  531. }
  532. s += end;
  533. summary.innerHTML = s;
  534. window.scrollTo(0,0);
  535. }
  536. if (summary.showmessagebox == "True") {
  537. s = "";
  538. if (typeof(summary.headertext) == "string") {
  539. s += summary.headertext + "\r\n";
  540. }
  541. var lastValIndex = Page_Validators.length - 1;
  542. for (i=0; i<=lastValIndex; i++) {
  543. if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
  544. switch (summary.displaymode) {
  545. case "List":
  546. s += Page_Validators[i].errormessage;
  547. if (i < lastValIndex) {
  548. s += "\r\n";
  549. }
  550. break;
  551. case "BulletList":
  552. default:
  553. s += "- " + Page_Validators[i].errormessage;
  554. if (i < lastValIndex) {
  555. s += "\r\n";
  556. }
  557. break;
  558. case "SingleParagraph":
  559. s += Page_Validators[i].errormessage + " ";
  560. break;
  561. }
  562. }
  563. }
  564. alert(s);
  565. }
  566. }
  567. }
  568. }
  569.  
  570. if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement