checkemail.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. *本插件原作者Vanadium,原文请移步前往http://vanadiumjs.com/查看
  3. *本插件由Mr.Think中文整理,Mr.Think的博客:http://MrThink.net/
  4. *转载及使用请务必注明原作者.
  5. */
  6. //弹出信息样式设置
  7. Vanadium.config = {
  8. valid_class: 'inputSus',//验证正确时表单样式
  9. invalid_class: 'erroT',//验证失败时该表单样式
  10. message_value_class: 'msgvaluecss',//这个样式是弹出信息中调用值的样式
  11. advice_class: '',//验证失败时文字信息的样式
  12. prefix: ':',
  13. separator: ';',
  14. reset_defer_timeout: 100
  15. }
  16. //验证类型及弹出信息设置
  17. Vanadium.Type = function(className, validationFunction, error_message, message, init) {
  18. this.initialize(className, validationFunction, error_message, message, init);
  19. };
  20. Vanadium.Type.prototype = {
  21. initialize: function(className, validationFunction, error_message, message, init) {
  22. this.className = className;
  23. this.message = message;
  24. this.error_message = error_message;
  25. this.validationFunction = validationFunction;
  26. this.init = init;
  27. },
  28. test: function(value) {
  29. return this.validationFunction.call(this, value);
  30. },
  31. validMessage: function() {
  32. return this.message;
  33. },
  34. invalidMessage: function() {
  35. return this.error_message;
  36. },
  37. toString: function() {
  38. return "className:" + this.className + " message:" + this.message + " error_message:" + this.error_message
  39. },
  40. init: function(parameter) {
  41. if (this.init) {
  42. this.init(parameter);
  43. }
  44. }
  45. };
  46. Vanadium.setupValidatorTypes = function() {
  47. Vanadium.addValidatorType('empty', function(v) {
  48. return ((v == null) || (v.length == 0));
  49. });
  50. //***************************************以下为验证方法,使用时可仅保留用到的判断
  51. Vanadium.addValidatorTypes([
  52. //是否为空
  53. ['required', function(v) {
  54. return !Vanadium.validators_types['empty'].test(v);
  55. }, '此项不可为空!'],
  56. //强制选中
  57. ['accept', function(v, _p, e) {
  58. return e.element.checked;
  59. }, '必须接受!'],
  60. //邮箱验证
  61. ['email', function (v) {
  62. return (Vanadium.validators_types['empty'].test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v))
  63. }, '格式错误,例如xxx@xxx.xxx'],
  64. //输入固定长度字符串
  65. ['length',
  66. function (v, p) {
  67. if (p === undefined) {
  68. return true
  69. } else {
  70. return v.length == parseInt(p)
  71. }
  72. ;
  73. },
  74. function (_v, p) {
  75. return '输入字符长度等于<span class="' + Vanadium.config.message_value_class + '">' + p + '</span>个.'
  76. }
  77. ],
  78. //
  79. ['min_length',
  80. function (v, p) {
  81. if (p === undefined) {
  82. return true
  83. } else {
  84. return v.length >= parseInt(p)
  85. }
  86. ;
  87. },
  88. function (_v, p) {
  89. return '输入字符长度不低于<span class="' + Vanadium.config.message_value_class + '">' + p + '</span>个.'
  90. }
  91. ],
  92. ['max_length',
  93. function (v, p) {
  94. if (p === undefined) {
  95. return true
  96. } else {
  97. return v.length <= parseInt(p)
  98. }
  99. ;
  100. },
  101. function (_v, p) {
  102. return '输入字符长度不大于<span class="' + Vanadium.config.message_value_class + '">' + p + '</span>个.'
  103. }
  104. ],
  105. //判断密码是相同
  106. ['same_as',
  107. function (v, p) {
  108. if (p === undefined) {
  109. return true
  110. } else {
  111. var exemplar = document.getElementById(p);
  112. if (exemplar)
  113. return v == exemplar.value;
  114. else
  115. return false;
  116. }
  117. ;
  118. },
  119. function (_v, p) {
  120. var exemplar = document.getElementById(p);
  121. if (exemplar)
  122. return '两次密码输入不相同.';
  123. else
  124. return '没有可参考值!'
  125. },
  126. "",
  127. function(validation_instance) {
  128. var exemplar = document.getElementById(validation_instance.param);
  129. if (exemplar){
  130. jQuery(exemplar).bind('validate', function(){
  131. jQuery(validation_instance.element).trigger('validate');
  132. });
  133. }
  134. }
  135. ],
  136. //ajax判断是否存在值
  137. ['ajax',
  138. function (v, p, validation_instance, decoration_context, decoration_callback) {
  139. if (Vanadium.validators_types['empty'].test(v)) return true;
  140. if (decoration_context && decoration_callback) {
  141. jQuery.getJSON(p, {
  142. value: v,
  143. id: validation_instance.element.id
  144. }, function(data) {
  145. decoration_callback.apply(decoration_context, [[data], true]);
  146. });
  147. }
  148. return true;
  149. }]
  150. ])
  151. if (typeof(VanadiumCustomValidationTypes) !== "undefined" && VanadiumCustomValidationTypes) Vanadium.addValidatorTypes(VanadiumCustomValidationTypes);
  152. };