weui-prompt.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //输入验证码
  2. // options = {
  3. // text: String,
  4. // title: String,
  5. // inputPhone: String 或 true
  6. // placeholder: String 或者 {phone: String, verifycode: String}
  7. // okText: String,
  8. // cancelText: String,
  9. // btnText: String,
  10. // delay: Number,
  11. // onOK: Function(text),
  12. // onCancel: Function,
  13. // onSend: Function,
  14. // verifyImg: String //图片验证码url
  15. // }
  16. function promptVC(options) {
  17. var countdown = options.delay || 60;
  18. var settime = function (obj) {
  19. if (countdown == 0) {
  20. $(obj).removeAttr("disabled");
  21. $(obj).removeClass('weui-btn_disabled')
  22. $(obj).text("获取");
  23. countdown = options.delay || 60;
  24. return;
  25. } else {
  26. console.log("剩余(" + countdown + "秒)");
  27. $(obj).attr("disabled", "disabled");
  28. $(obj).addClass('weui-btn_disabled')
  29. $(obj).text("(" + countdown + "秒)");
  30. countdown--;
  31. setTimeout(function () {
  32. settime(obj)
  33. }, 1000)
  34. }
  35. }
  36. if (options.placeholder && options.placeholder instanceof String) {
  37. options.placeholder = {
  38. verifycode: options.placeholder
  39. }
  40. }
  41. var content = '<p>' + options.text + '</p>'
  42. if (options.inputPhone === true) {
  43. content += '<div class="weui-prompt-box"><input placeholder="' + ((options.placeholder && options.placeholder.phone) || '请输入手机号') + '" class="weui-prompt-input weui-input" id="weui-prompt-phone"/></div>'
  44. } else if (options.inputPhone && typeof options.inputPhone == "string") {
  45. content += '<div class="weui-prompt-box"><input value="' + options.inputPhone + '" class="weui-prompt-input weui-input" id="weui-prompt-phone" readonly="readonly"/></div>'
  46. }
  47. content += '<div class="weui-prompt-box"><input placeholder="' + ((options.placeholder && options.placeholder.verifycode) || '请输入验证码') + '" class="weui-prompt-input weui-input hasbtn" id="weui-prompt-verifycode"/>' +
  48. '<button class="weui-prompt-button weui-btn weui-btn_mini weui-btn_default" id="weui-prompt-button">' + (options.btnText || '获取') + '</button></div>'
  49. if (options.verifyImg) {
  50. content += '<div class="weui-prompt-box"><input placeholder="' + ((options.placeholder && options.placeholder.verifyimg) || '图片验证码') + '" class="weui-prompt-input weui-input hasbtn" id="weui-prompt-verifyimg"/>' +
  51. '<img class="weui-prompt-button weui-btn weui-btn_mini weui-btn_default" src="' + options.verifyImg + '"></img></div>'
  52. }
  53. var checkInput = function (input, message, regexp) {
  54. if (input.val() === "" || input.val() === null) {
  55. input.focus()[0].select();
  56. weui.topTips(message || '请填写正确的字段');
  57. return false;
  58. }
  59. if (regexp && regexp instanceof RegExp && !regexp.test(input.val())) {
  60. input.focus()[0].select();
  61. weui.topTips(message || '请填写正确的字段');
  62. return false;
  63. }
  64. return true;
  65. }
  66. var dlgOpts = {
  67. title: options.title,
  68. content: content,
  69. buttons: [{
  70. label: options.cancelText || '取消',
  71. type: 'default',
  72. onClick: function () {
  73. if (options.onCancel && (options.onCancel.call(dlg) == false))
  74. return false;
  75. countdown = 0;
  76. }
  77. }, {
  78. label: options.okText || '确定',
  79. type: 'primary',
  80. onClick: function () {
  81. var val = {};
  82. var input;
  83. //TODO: 检查手机号
  84. //134,135,136,137,138,139,147,150,151,152,157,158,159,178,182,183,184,187,188
  85. var regex = /^(13[4-9]|147|15[0-27-9]|178|198|18[23478])\d{8}$/; /*/^1[3-8]\d{9}$/*/
  86. if (options.inputPhone == true) {
  87. if (!checkInput(input = $(dlg).find("#weui-prompt-phone"), '请输入有效的手机号', regex))
  88. return false;
  89. else
  90. val.phone = input.val();
  91. } else if (typeof options.inputPhone == "string" && regex.test(options.inputPhone)) {
  92. val.phone = options.inputPhone;
  93. }
  94. //TODO: 检查验证码
  95. if (!checkInput(input = $(dlg).find("#weui-prompt-verifycode"), '请输入有效的短信验证码', /^\d{6}$/)) {
  96. return false;
  97. } else
  98. val.verifyCode = input.val();
  99. //TODO: 检查图片验证码
  100. if (options.verifyImg) {
  101. if (!checkInput(input = $(dlg).find("#weui-prompt-verifyimg"), '请输入有效的图片验证码', /^[A-z0-9]{4}$/))
  102. return false;
  103. else
  104. val.verifyImg = input.val();
  105. }
  106. if (options.onOK && (options.onOK.call(dlg, val) == false))
  107. return false;
  108. countdown = 0;
  109. }
  110. }]
  111. };
  112. if (options.cancelText == 'disabled') {
  113. dlgOpts.buttons.shift();
  114. }
  115. if (options.isAndroid != undefined)
  116. dlgOpts.isAndroid = options.isAndroid;
  117. var dlg = weui.dialog(dlgOpts);
  118. var btn = $(dlg).find('#weui-prompt-button');
  119. btn.click(function () {
  120. var phone, input;
  121. //TODO: 检查手机号
  122. var regex = /^(13[4-9]|147|15[0-27-9]|178|198|18[23478])\d{8}$/; /*/^1[3-8]\d{9}$/*/
  123. if (options.inputPhone == true) {
  124. if (!checkInput(input = $(dlg).find("#weui-prompt-phone"), '请输入有效的手机号', regex)) {
  125. return false;
  126. } else
  127. phone = input.val()
  128. } else if (typeof options.inputPhone == "string" && regex.test(options.inputPhone)) {
  129. phone = options.inputPhone;
  130. }
  131. if (options.onSend && (options.onSend.call(dlg, phone) != false))
  132. settime(this);
  133. });
  134. }