ValidationTextbox.js
上传用户:kimgenplus
上传日期:2016-06-05
资源大小:20877k
文件大小:5k
源码类别:

OA系统

开发平台:

Java

  1. /*
  2. Copyright (c) 2004-2006, The Dojo Foundation
  3. All Rights Reserved.
  4. Licensed under the Academic Free License version 2.1 or above OR the
  5. modified BSD license. For more information on Dojo licensing, see:
  6. http://dojotoolkit.org/community/licensing.shtml
  7. */
  8. dojo.provide("dojo.widget.ValidationTextbox");
  9. dojo.require("dojo.widget.Textbox");
  10. dojo.require("dojo.i18n.common");
  11. dojo.widget.defineWidget("dojo.widget.ValidationTextbox", dojo.widget.Textbox, function () {
  12. this.flags = {};
  13. }, {required:false, rangeClass:"range", invalidClass:"invalid", missingClass:"missing", classPrefix:"dojoValidate", size:"", maxlength:"", promptMessage:"", invalidMessage:"", missingMessage:"", rangeMessage:"", listenOnKeyPress:true, htmlfloat:"none", lastCheckedValue:null, templateString:"<span style='float:${this.htmlfloat};'>nt<input dojoAttachPoint='textbox' type='${this.type}' dojoAttachEvent='onblur;onfocus;onkeyup'nttid='${this.widgetId}' name='${this.name}' size='${this.size}' maxlength='${this.maxlength}'nttclass='${this.className}' style=''>nt<span dojoAttachPoint='invalidSpan' class='${this.invalidClass}'>${this.messages.invalidMessage}</span>nt<span dojoAttachPoint='missingSpan' class='${this.missingClass}'>${this.messages.missingMessage}</span>nt<span dojoAttachPoint='rangeSpan' class='${this.rangeClass}'>${this.messages.rangeMessage}</span>n</span>n", templateCssString:".dojoValidateEmpty{ntbackground-color: #00FFFF;n}n.dojoValidateValid{ntbackground-color: #cfc;n}n.dojoValidateInvalid{ntbackground-color: #fcc;n}n.dojoValidateRange{ntbackground-color: #ccf;n}n", templateCssPath:dojo.uri.moduleUri("dojo.widget", "templates/Validate.css"), invalidSpan:null, missingSpan:null, rangeSpan:null, getValue:function () {
  14. return this.textbox.value;
  15. }, setValue:function (value) {
  16. this.textbox.value = value;
  17. this.update();
  18. }, isValid:function () {
  19. return true;
  20. }, isInRange:function () {
  21. return true;
  22. }, isEmpty:function () {
  23. return (/^s*$/.test(this.textbox.value));
  24. }, isMissing:function () {
  25. return (this.required && this.isEmpty());
  26. }, update:function () {
  27. this.lastCheckedValue = this.textbox.value;
  28. this.missingSpan.style.display = "none";
  29. this.invalidSpan.style.display = "none";
  30. this.rangeSpan.style.display = "none";
  31. var empty = this.isEmpty();
  32. var valid = true;
  33. if (this.promptMessage != this.textbox.value) {
  34. valid = this.isValid();
  35. }
  36. var missing = this.isMissing();
  37. if (missing) {
  38. this.missingSpan.style.display = "";
  39. } else {
  40. if (!empty && !valid) {
  41. this.invalidSpan.style.display = "";
  42. } else {
  43. if (!empty && !this.isInRange()) {
  44. this.rangeSpan.style.display = "";
  45. }
  46. }
  47. }
  48. this.highlight();
  49. }, updateClass:function (className) {
  50. var pre = this.classPrefix;
  51. dojo.html.removeClass(this.textbox, pre + "Empty");
  52. dojo.html.removeClass(this.textbox, pre + "Valid");
  53. dojo.html.removeClass(this.textbox, pre + "Invalid");
  54. dojo.html.addClass(this.textbox, pre + className);
  55. }, highlight:function () {
  56. if (this.isEmpty()) {
  57. this.updateClass("Empty");
  58. } else {
  59. if (this.isValid() && this.isInRange()) {
  60. this.updateClass("Valid");
  61. } else {
  62. if (this.textbox.value != this.promptMessage) {
  63. this.updateClass("Invalid");
  64. } else {
  65. this.updateClass("Empty");
  66. }
  67. }
  68. }
  69. }, onfocus:function (evt) {
  70. if (!this.listenOnKeyPress) {
  71. this.updateClass("Empty");
  72. }
  73. }, onblur:function (evt) {
  74. this.filter();
  75. this.update();
  76. }, onkeyup:function (evt) {
  77. if (this.listenOnKeyPress) {
  78. this.update();
  79. } else {
  80. if (this.textbox.value != this.lastCheckedValue) {
  81. this.updateClass("Empty");
  82. }
  83. }
  84. }, postMixInProperties:function (localProperties, frag) {
  85. dojo.widget.ValidationTextbox.superclass.postMixInProperties.apply(this, arguments);
  86. this.messages = dojo.i18n.getLocalization("dojo.widget", "validate", this.lang);
  87. dojo.lang.forEach(["invalidMessage", "missingMessage", "rangeMessage"], function (prop) {
  88. if (this[prop]) {
  89. this.messages[prop] = this[prop];
  90. }
  91. }, this);
  92. }, fillInTemplate:function () {
  93. dojo.widget.ValidationTextbox.superclass.fillInTemplate.apply(this, arguments);
  94. this.textbox.isValid = function () {
  95. this.isValid.call(this);
  96. };
  97. this.textbox.isMissing = function () {
  98. this.isMissing.call(this);
  99. };
  100. this.textbox.isInRange = function () {
  101. this.isInRange.call(this);
  102. };
  103. dojo.html.setClass(this.invalidSpan, this.invalidClass);
  104. this.update();
  105. this.filter();
  106. if (dojo.render.html.ie) {
  107. dojo.html.addClass(this.domNode, "ie");
  108. }
  109. if (dojo.render.html.moz) {
  110. dojo.html.addClass(this.domNode, "moz");
  111. }
  112. if (dojo.render.html.opera) {
  113. dojo.html.addClass(this.domNode, "opera");
  114. }
  115. if (dojo.render.html.safari) {
  116. dojo.html.addClass(this.domNode, "safari");
  117. }
  118. }});