场景一、有一个输入金额的场景,这个金额需要验证,验证说明如下:

不能为空格;

不能为0;

不能为汉字;

不能为其它字符;

不能大于200;

唯一可以的是,只有输入3~199之间的数字,下面的确定按钮才会显示,否则,隐藏这个按钮。

HTML:

  1. <!--医生问诊金额-->
  2. <div class="weui-jiaj-panel">
  3. <div class="weui-jiaj-money-box dialog js_show">
  4. <div class="weui-jiaj-money-box-btn">
  5.  
  6. </div>
  7. <div class="weui-jiaj-money-box-three">
  8. <div class="weui-flex__item">
  9. <a id="showMoney" href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default">其它</a>
  10. </div>
  11. </div>
  12. </div>
  13. </div>
  14. <!--其它金额-->
  15. <div class="weui_dialog_alert" id="showMoneyDialog" style="display: none;">
  16. <div class="weui_mask"></div>
  17. <div class="weui_dialog">
  18. <div class="weui_dialog_hd"><strong class="weui_dialog_title">其它金额</strong></div>
  19. <div class="weui_dialog_bd">
  20. <div class="weui-jiaj-dialog-panel">
  21. <div class="weui-cell">
  22. <div class="weui-cell__bd">
  23. <input id="dialogPrice" type="text" required class="weui-input" placeholder="¥10" />
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. <div class="weui_dialog_ft">
  29. <div id="otherPriceBtn" class="weui_btn_dialog primary">确定</div>
  30. </div>
  31. </div>
  32. </div>

JS:

  1. <script>
  2. //设置其它金额
  3. var doctorPrices = [{
  4. "doctorPrice": "5"
  5. }, {
  6. "doctorPrice": "10"
  7. }, {
  8. "doctorPrice": "15"
  9. }, {
  10. "doctorPrice": "20"
  11. }, {
  12. "doctorPrice": "30"
  13. }, {
  14. "doctorPrice": "60"
  15. }];
  16.  
  17. var userId = $.cookie('doctorId');
  18.  
  19. $(function() {
  20. selectedPrice();
  21. });
  22.  
  23. var page = $('.page'); //顶层div
  24. var panel = page.find('weui-jiaj-panel');
  25.  
  26. function selectedPrice() {
  27. var $titleHtml = '';
  28. for(var a = 0; a < doctorPrices.length; a++) {
  29. var priceName = doctorPrices[a].doctorPrice;
  30. //点周weui_btn_dialog隐藏
  31. $titleHtml += '<button class="price_btn weui-btn weui-btn_mini weui-btn_warn"' + 'name=' + priceName + '>' + priceName + '</button>';
  32. $('.price_btn').css('margin', '5px');
  33. }
  34. $('.weui-jiaj-money-box-btn').append($titleHtml);
  35.  
  36. //选择金额
  37. $('.price_btn').click(function() {
  38. var titleValue = $(this).attr('name'); //$(this)表示获取当前被点击元素的name值
  39.  
  40. var data = {
  41. userId: userId,
  42. price: titleValue
  43. };
  44.  
  45. data = JSON.stringify(data);
  46. $.ajax({
  47. data: {},
  48. dataType: 'json',
  49. type: "post",
  50. url: postDoctorPrice().replace("{userId}", userId).replace("{price}", titleValue),
  51. contentType: 'application/json; charset=utf-8',
  52. success: function(data) {
  53. if(data && data.status == '200') {
  54. weui.topTips('提交成功');
  55. }
  56. },
  57. error: function(data) {
  58. location.href = 'doctor_wode.html';
  59. }
  60. });
  61. });
  62.  
  63. //其它金额
  64. $('#otherPriceBtn').on('click', function(e) {
  65. var otherPrice = $('#dialogPrice').val();
  66. otherPrice = parseInt(otherPrice);
  67.  
  68. otherPrice = otherPrice.toString();
  69. console.log("其它金额" + otherPrice);
  70. var data = {
  71. userId: userId,
  72. price: otherPrice
  73. };
  74.  
  75. data = JSON.stringify(data);
  76. $.ajax({
  77. data: {},
  78. dataType: 'json',
  79. type: "post",
  80. url: postDoctorPrice().replace("{userId}", userId).replace("{price}", otherPrice), //post 时url带参数
  81. contentType: 'application/json; charset=utf-8',
  82. success: function(data) {
  83. if(data && data.status == '200') {
  84. weui.topTips('设置成功!');
  85. }
  86. },
  87. error: function(data) {
  88. location.href = 'doctor_wode.html';
  89. }
  90. });
  91. });
  92. }
  93.  
  94. //验证
  95. $('input').on('blur',function(){
  96. var value = this.value;
  97. var regChinese = new RegExp("[\\u4E00-\\u9FFF]+","g");
  98. //字符串不能为空
  99. if(value.length == 0) {
  100. $('#otherPriceBtn').hide();
  101. weui.topTips('不能为空');
  102. //字符串是否为“空”字符即用户输入了空格
  103. }else if(value.replace(/(^s*)|(s*$)/g, "").length ==0){
  104. $('#otherPriceBtn').hide();
  105. weui.topTips('不能为空');
  106. //字符串是否为空或者全部都是空格
  107. }else if(value == null){
  108. $('#otherPriceBtn').hide();
  109. weui.topTips('不能为null');
  110. //字符串是否为汉字
  111. }else if(regChinese.test(value)){
  112. $('#otherPriceBtn').hide();
  113. weui.topTips('不能输入汉字');
  114. //字符串不能为0
  115. }else if(parseInt(value) == 0){
  116. $('#otherPriceBtn').hide();
  117. weui.topTips('不能为0');
  118. //不能大于200
  119. }else if(parseInt(value) > 200){
  120. $('#otherPriceBtn').hide();
  121. weui.topTips('自定义金额不能大于200元');
  122. //自定义金额只能是数字
  123. }else if(typeof(parseInt(value))){
  124. $('#otherPriceBtn').show();
  125. }
  126. })
  127. </script>

场景二、所有违反规距的都有信息提示,但是“确定”按钮不隐藏,只是删除它的click事件,只有符合条件的才可以跳转

  1. //验证
  2. $('input').on('blur', function() {
  3. var value = this.value;
  4. var regChinese = new RegExp("[\\u4E00-\\u9FFF]+", "g"); //汉语
  5. var specialSymbol =/[`~!@#$%^&*_+<>{}\/'[\]]/im; //特殊符号
  6. //字符串不能为空
  7. if(value.length == 0) {
  8. $('#otherPriceBtn').unbind('click');
  9. setTimeout(function() {
  10. $('.hide-description').css('display', 'block').text('不能为空,请重新输入');
  11. }, 500);
  12. //字符串是否为“空”字符即用户输入了空格
  13. } else if(value.replace(/(^s*)|(s*$)/g, "").length == 0) {
  14. $('#otherPriceBtn').unbind('click');
  15. setTimeout(function() {
  16. $('.hide-description').css('display', 'block').text('不能为空,请重新输入');
  17. }, 500);
  18. //字符串是否为空或者全部都是空格
  19. } else if(value == null) {
  20. $('#otherPriceBtn').unbind('click');
  21. setTimeout(function() {
  22. $('.hide-description').css('display', 'block').text('不能为空,请重新输入');
  23. }, 500);
  24. //字符串是否为汉字
  25. } else if(regChinese.test(value)) {
  26. $('#otherPriceBtn').unbind('click');
  27. setTimeout(function() {
  28. $('.hide-description').css('display', 'block').text('不能输入汉字,请重新输入');
  29. }, 500);
  30. //字符串不能为0
  31. } else if(parseInt(value) == 0) {
  32. $('#otherPriceBtn').unbind('click');
  33. setTimeout(function() {
  34. $('.hide-description').css('display', 'block').text('不能为0,请重新输入');
  35. }, 500);
  36. //小于3
  37. } else if(parseInt(value) < 4) {
  38. $('#otherPriceBtn').unbind('click');
  39. setTimeout(function() {
  40. $('.hide-description').css('display', 'block').text('自定义金额不能小于3,请重新输入');
  41. }, 500);
  42. //不能大于200
  43. } else if(parseInt(value) > 200) {
  44. $('#otherPriceBtn').unbind('click');
  45. setTimeout(function() {
  46. $('.hide-description').css('display', 'block').text('自定义金额不能大于200,请重新输入');
  47. }, 500);
  48. } else if(specialSymbol.test(value)){
  49. //禁止输入特殊字符
  50. $('#otherPriceBtn').unbind('click');
  51. setTimeout(function() {
  52. $('.hide-description').css('display', 'block').text('不可输入!@#¥%……&*特殊字符!');
  53. }, 500);
  54. //自定义金额只能是数字
  55. } else if(typeof(parseInt(value))) {
  56. setTimeout(function() {
  57. $('.hide-description').css('display', 'block').text('你设置的金额为' + value);
  58. }, 500);
  59. //其它金额
  60. $('#otherPriceBtn').on('click', function(e) {
  61. var otherPrice = $('#dialogPrice').val();
  62. otherPrice = parseInt(otherPrice);
  63.  
  64. otherPrice = otherPrice.toString();
  65. console.log("其它金额" + otherPrice);
  66. var data = {
  67. userId: userId,
  68. price: otherPrice
  69. };
  70.  
  71. data = JSON.stringify(data);
  72. $.ajax({
  73. data: {},
  74. dataType: 'json',
  75. type: "post",
  76. url: postDoctorPrice().replace("{userId}", userId).replace("{price}", otherPrice), //post 时url带参数
  77. contentType: 'application/json; charset=utf-8',
  78. success: function(data) {
  79. if(data && data.status == '200') {
  80. weui.topTips('设置成功!');
  81. }
  82. },
  83. error: function(data) {
  84. location.href = 'doctor_wode.html';
  85. }
  86. });
  87. });
  88. }
  89. })

巧用weui.topTips验证数据的更多相关文章

  1. 【C#】让工具栏ToolStrip能触发焦点控件的Leave、Validating、DataError等事件以验证数据

    ----------------更新:2014-04-21--------------- 蒙doggo兄指教,得知有更好的方法可以代替蹩脚的0尺寸Button法,即调用窗体的验证方法Form.Vali ...

  2. 微信支付java版V3验证数据合法性

    [TOC] 1. 微信支付java版V3验证数据合法性 概要:使用微信支付接口时,微信会返回或回调给商户XML数据,开发者需要验证微信返回的数据是否合法. 特别提醒:商户系统对于支付结果通知的内容一定 ...

  3. RSA签名和验证数据

    private const string PubKey = "BgIAAACkAABSU0ExAAQAAAEAAQAxg/L6l3AyA+Zd7Hm7ESCcS4CcgY8PvwE2arRv ...

  4. JSR303中的来验证数据信息

    spring mvc之实现简单的用户管理三 博客分类: spring spring mvc spring mvc dispatcherServlet springspring mvcbean vali ...

  5. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  6. vue props 下有验证器 validator 验证数据返回true false后,false给default值

    vue props 下有验证器 validator 验证数据返回true false后,false给default值 props: { type: { validator (value) { retu ...

  7. js案例之使用正则表达式进行验证数据正确性

    #js案例之使用正则表达式进行验证数据正确性 代码上传至 "GitHub" 样例: <tr> <td>密码:</td> <td> & ...

  8. mongoose 更新数据时不验证数据(忽略设定的集合规则)的问题

    问题: mongoose 更新数据时不验证数据(忽略设定的集合规则)的问题 参考: http://www.mongoosejs.net/docs/api.html#updateone_updateOn ...

  9. Struts2(十二)使用验证框架验证数据较验

    一.数据验证 1.1.为什么要进行数据验证 对数据的合法性进行检查,只允许合法的数据进入应用程序 1.2.在哪里实现数据验证 客户端验证: 数据提交前在客户端验证 可使用JavaScript或者JQu ...

随机推荐

  1. oc 导航栏跳转指定界面

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIn ...

  2. Code Sign error: No code signing identities found: No valid signing identities

    Code Sign error: No code signing identities found: No valid signing identities 解决办法:如果证书可获取,最简办法就是把所 ...

  3. SQL语句优化技术分析 整理他人的

    一.操作符优化 1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用 ...

  4. Python学习之旅--第二周--python基础

    一.什么是pyc? 1.Python是解释性语言,那么.pyc是什么文件? 2.解释性语言和编译型语言区别:    计算机是不能够识别高级语言的,所以当我们运行一个高级别语言程序时,就需要一个&quo ...

  5. javascript语言学习笔记。

    js类创建方法 var DogKing = function(dogName){ this.dogName = dogName; }; var myDogKing = new DogKing(&quo ...

  6. 深入了解——CSS3新增属性

    CSS3 选择器(Selector) 写过 CSS 的人应该对 CSS 选择器不陌生,我们所定义的 CSS 属性之所以能应用到相应的节点上,就是因为 CSS 选择器模式.参考下述代码: 清单 1. C ...

  7. Arrays.toString(a)--->将数组a的值转换为字符串

    Arrays.toString(数组)是java内置类Arrays类的一个方法,具体查Api可知.因为数组是不可直接输出的,它的作用是将数组转换为字符串.其实用for循环也是可以做到的,只不过比for ...

  8. line-height:2、line-height:2em、line-height:200%的区别

    文章来源: http://www.zhihu.com/question/20394889 总结: 1.line-height:2em.line-height:200%  根据父元素的字体大小计算行高 ...

  9. Node.js:模块

    概要:本篇博客主要介绍node.js的模块 1.创建模块 在node.js中创建一个模块非常简单,因为一个文件就是一个模块.我们只需要明白如何从其他文件中获取这个模块.Node.js提供了 expor ...

  10. <hr/>标签改变颜色注意事项

    1.css改变颜色 <hr style="border:0;background-color:#093;height:1px;">   注意: 如果不加border:0 ...