巧用weui.topTips验证数据的实例


Posted in Javascript onApril 17, 2017

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

不能为空格;

不能为0;

不能为汉字;

不能为其它字符;

不能大于200;

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

HTML:

<!--医生问诊金额-->
        <div class="weui-jiaj-panel">
          <div class="weui-jiaj-money-box dialog js_show">
            <div class="weui-jiaj-money-box-btn">

            </div>
            <div class="weui-jiaj-money-box-three">
              <div class="weui-flex__item">
                <a id="showMoney" href="javascript:;" rel="external nofollow" class="weui-btn weui-btn_mini weui-btn_default">其它</a>
              </div>
            </div>
          </div>
        </div>
        <!--其它金额-->
        <div class="weui_dialog_alert" id="showMoneyDialog" style="display: none;">
          <div class="weui_mask"></div>
          <div class="weui_dialog">
            <div class="weui_dialog_hd"><strong class="weui_dialog_title">其它金额</strong></div>
            <div class="weui_dialog_bd">
              <div class="weui-jiaj-dialog-panel">
                <div class="weui-cell">
                  <div class="weui-cell__bd">
                    <input id="dialogPrice" type="text" required class="weui-input" placeholder="¥10" />
                  </div>
                </div>
              </div>
            </div>
            <div class="weui_dialog_ft">
              <div id="otherPriceBtn" class="weui_btn_dialog primary">确定</div>
            </div>
          </div>
        </div>

JS:

<script>
      //设置其它金额
      var doctorPrices = [{
        "doctorPrice": "5"
      }, {
        "doctorPrice": "10"
      }, {
        "doctorPrice": "15"
      }, {
        "doctorPrice": "20"
      }, {
        "doctorPrice": "30"
      }, {
        "doctorPrice": "60"
      }];

      var userId = $.cookie('doctorId');

      $(function() {
        selectedPrice();
      });

      var page = $('.page'); //顶层div
      var panel = page.find('weui-jiaj-panel');

      function selectedPrice() {
        var $titleHtml = '';
        for(var a = 0; a < doctorPrices.length; a++) {
          var priceName = doctorPrices[a].doctorPrice;
          //点周weui_btn_dialog隐藏
          $titleHtml += '<button class="price_btn weui-btn weui-btn_mini weui-btn_warn"' + 'name=' + priceName + '>' + priceName + '</button>';
          $('.price_btn').css('margin', '5px');
        }
        $('.weui-jiaj-money-box-btn').append($titleHtml);

        //选择金额
        $('.price_btn').click(function() {
          var titleValue = $(this).attr('name'); //$(this)表示获取当前被点击元素的name值

          var data = {
            userId: userId,
            price: titleValue
          };

          data = JSON.stringify(data);
          $.ajax({
            data: {},
            dataType: 'json',
            type: "post",
            url: postDoctorPrice().replace("{userId}", userId).replace("{price}", titleValue),
            contentType: 'application/json; charset=utf-8',
            success: function(data) {
              if(data && data.status == '200') {
                weui.topTips('提交成功');
              }
            },
            error: function(data) {
              location.href = 'doctor_wode.html';
            }
          });
        });

        //其它金额
        $('#otherPriceBtn').on('click', function(e) {
          var otherPrice = $('#dialogPrice').val();
          otherPrice = parseInt(otherPrice);

          otherPrice = otherPrice.toString();
          console.log("其它金额" + otherPrice);
          var data = {
            userId: userId,
            price: otherPrice
          };

          data = JSON.stringify(data);
          $.ajax({
            data: {},
            dataType: 'json',
            type: "post",
            url: postDoctorPrice().replace("{userId}", userId).replace("{price}", otherPrice), //post 时url带参数
            contentType: 'application/json; charset=utf-8',
            success: function(data) {
              if(data && data.status == '200') {
                weui.topTips('设置成功!');
              }
            },
            error: function(data) {
              location.href = 'doctor_wode.html';
            }
          });
        });
      }

      //验证
      $('input').on('blur',function(){
        var value = this.value;
        var regChinese = new RegExp("[\\u4E00-\\u9FFF]+","g");
        //字符串不能为空
        if(value.length == 0) {
          $('#otherPriceBtn').hide();
          weui.topTips('不能为空');
          //字符串是否为“空”字符即用户输入了空格
        }else if(value.replace(/(^s*)|(s*$)/g, "").length ==0){
          $('#otherPriceBtn').hide();
          weui.topTips('不能为空');
          //字符串是否为空或者全部都是空格
        }else if(value == null){
          $('#otherPriceBtn').hide();
          weui.topTips('不能为null');
          //字符串是否为汉字
        }else if(regChinese.test(value)){
          $('#otherPriceBtn').hide();
          weui.topTips('不能输入汉字');
          //字符串不能为0
        }else if(parseInt(value) == 0){
          $('#otherPriceBtn').hide();
          weui.topTips('不能为0');
          //不能大于200
        }else if(parseInt(value) > 200){
          $('#otherPriceBtn').hide();
          weui.topTips('自定义金额不能大于200元');
          //自定义金额只能是数字
        }else if(typeof(parseInt(value))){
          $('#otherPriceBtn').show();
        }
      })
    </script>

巧用weui.topTips验证数据的实例巧用weui.topTips验证数据的实例

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

//验证
      $('input').on('blur', function() {
        var value = this.value;
        var regChinese = new RegExp("[\\u4E00-\\u9FFF]+", "g"); //汉语
        var specialSymbol =/[`~!@#$%^&*_+<>{}\/'[\]]/im; //特殊符号
        //字符串不能为空
        if(value.length == 0) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('不能为空,请重新输入');
          }, 500);
          //字符串是否为“空”字符即用户输入了空格
        } else if(value.replace(/(^s*)|(s*$)/g, "").length == 0) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('不能为空,请重新输入');
          }, 500);
          //字符串是否为空或者全部都是空格
        } else if(value == null) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('不能为空,请重新输入');
          }, 500);
          //字符串是否为汉字
        } else if(regChinese.test(value)) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('不能输入汉字,请重新输入');
          }, 500);
          //字符串不能为0
        } else if(parseInt(value) == 0) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('不能为0,请重新输入');
          }, 500);
          //小于3
        } else if(parseInt(value) < 4) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('自定义金额不能小于3,请重新输入');
          }, 500);
          //不能大于200
        } else if(parseInt(value) > 200) {
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('自定义金额不能大于200,请重新输入');
          }, 500);
        } else if(specialSymbol.test(value)){
          //禁止输入特殊字符
          $('#otherPriceBtn').unbind('click');
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('不可输入!@#¥%……&*特殊字符!');
          }, 500);
          //自定义金额只能是数字
        } else if(typeof(parseInt(value))) {
          setTimeout(function() {
            $('.hide-description').css('display', 'block').text('你设置的金额为' + value);
          }, 500);
          //其它金额
          $('#otherPriceBtn').on('click', function(e) {
            var otherPrice = $('#dialogPrice').val();
            otherPrice = parseInt(otherPrice);

            otherPrice = otherPrice.toString();
            console.log("其它金额" + otherPrice);
            var data = {
              userId: userId,
              price: otherPrice
            };

            data = JSON.stringify(data);
            $.ajax({
              data: {},
              dataType: 'json',
              type: "post",
              url: postDoctorPrice().replace("{userId}", userId).replace("{price}", otherPrice), //post 时url带参数
              contentType: 'application/json; charset=utf-8',
              success: function(data) {
                if(data && data.status == '200') {
                  weui.topTips('设置成功!');
                }
              },
              error: function(data) {
                location.href = 'doctor_wode.html';
              }
            });
          });
        }
      })

以上这篇巧用weui.topTips验证数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jQuery 过滤not()与filter()实例代码
May 10 Javascript
jQuery中$.extend()用法实例
Jun 24 Javascript
基于PHP和Mysql相结合使用jqGrid读取数据并显示
Dec 02 Javascript
jQuery实现日期联动效果实例
Jul 26 Javascript
webpack+vue.js快速入门教程
Oct 12 Javascript
详解如何用webpack打包一个网站应用项目
Jul 12 Javascript
Angular中封装fancyBox(图片预览)遇到问题小结
Sep 01 Javascript
vue中阻止click事件冒泡,防止触发另一个事件的方法
Feb 08 Javascript
JavaScript 中定义函数用 var foo = function () {} 和 function foo()区别介绍
Mar 01 Javascript
Vue + Elementui实现多标签页共存的方法
Jun 12 Javascript
Vue-cli打包后部署到子目录下的路径问题说明
Sep 02 Javascript
react的hooks的用法详解
Oct 12 Javascript
JS与jQuery实现子窗口获取父窗口元素值的方法
Apr 17 #jQuery
bootstrap select插件封装成Vue2.0组件
Apr 17 #Javascript
JS简单获取当前日期和农历日期的方法
Apr 17 #Javascript
Vue.js仿Metronic高级表格(一)静态设计
Apr 17 #Javascript
react.js CMS 删除功能的实现方法
Apr 17 #Javascript
JS实现二叉查找树的建立以及一些遍历方法实现
Apr 17 #Javascript
简单的JS控制button颜色随点击更改的实现方法
Apr 17 #Javascript
You might like
用PHP实现将GB编码转换为UTF8
2006/11/25 PHP
php chr() ord()中文截取乱码问题解决方法
2008/09/08 PHP
PHP mkdir()定义和用法
2009/01/14 PHP
php 多线程上下文中安全写文件实现代码
2009/12/28 PHP
无JS,完全php面向过程数据分页实现代码
2012/08/27 PHP
php实现的短网址算法分享
2014/06/20 PHP
PHP+jQuery实现即点即改功能示例
2019/02/21 PHP
Wordpress ThickBox 添加“查看原图”效果代码
2010/12/11 Javascript
javascript操作select元素实例分析
2015/03/27 Javascript
JavaScript中函数表达式和函数声明及函数声明与函数表达式的不同
2015/11/15 Javascript
jquery中实现时间戳与日期相互转换
2016/04/12 Javascript
jQuery简单设置文本框回车事件的方法
2016/08/01 Javascript
Javascript 判断两个IP是否在同一网段实例代码
2016/11/28 Javascript
vue.js实现备忘录功能的方法
2017/07/10 Javascript
基于JavaScript实现弹幕特效
2020/08/27 Javascript
H5实现仿flash效果的实现代码
2017/09/29 Javascript
关闭Vue计算属性自带的缓存功能方法
2018/03/02 Javascript
vue-router 源码之实现一个简单的 vue-router
2018/07/02 Javascript
微信小程序点击保存图片到本机功能
2019/12/13 Javascript
Vue-cli3生成的Vue项目加载Mxgraph方法示例
2020/05/31 Javascript
Python切片操作深入详解
2018/07/27 Python
python tkinter实现屏保程序
2019/07/30 Python
简单了解Python write writelines区别
2020/02/27 Python
keras 权重保存和权重载入方式
2020/05/21 Python
Python3如何在服务器打印资产信息
2020/08/27 Python
python+selenium自动化实战携带cookies模拟登陆微博
2021/01/19 Python
html5触摸事件判断滑动方向的实现
2018/06/05 HTML / CSS
canvas仿写贝塞尔曲线的示例代码
2017/12/29 HTML / CSS
阿玛尼美国官方网站:Armani.com
2016/11/25 全球购物
体育教师工作总结的自我评价
2013/10/10 职场文书
社会实践自我鉴定
2013/11/07 职场文书
国际会计专业求职信
2014/08/04 职场文书
端午节活动总结
2014/08/26 职场文书
股东协议书范本2016
2016/03/21 职场文书
基于MySql验证的vsftpd虚拟用户
2021/11/07 MySQL
NodeJs使用webpack打包项目的方法详解
2022/02/28 NodeJs