巧用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 相关文章推荐
js字符编码函数区别分析
Jun 05 Javascript
javascript控制swfObject应用介绍
Nov 29 Javascript
探讨js中的双感叹号判断
Nov 11 Javascript
JavaScript设计模式之装饰者模式介绍
Dec 28 Javascript
超链接怎么正确调用javascript函数
May 23 Javascript
js传值后台中文出现乱码的解决方法
Jun 30 Javascript
利用JS实现scroll自定义滚动效果详解
Oct 17 Javascript
Vue使用zTree插件封装树组件操作示例
Apr 25 Javascript
vue cli3.0 引入eslint 结合vscode使用
May 27 Javascript
Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
Jun 24 Javascript
基于Vue SEO的四种方案(小结)
Jul 01 Javascript
微信小程序实现禁止分享代码实例
Oct 19 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中检查PHP文件是否有语法错误的方法
2009/12/23 PHP
php访问数组最后一个元素的函数end()用法
2015/03/18 PHP
Mac系统下安装PHP Xdebug
2018/03/30 PHP
PHP实现微信商户支付企业付款到零钱功能
2018/09/30 PHP
laravel-admin的图片删除实例
2019/09/30 PHP
使用Firebug对js进行断点调试的图文方法
2011/04/02 Javascript
JS完整获取IE浏览器信息包括类型、版本、语言等等
2014/05/22 Javascript
使用jQuery的attr方法来修改onclick值
2014/07/07 Javascript
javascript HTML5 Canvas实现圆盘抽奖功能
2016/04/11 Javascript
Jquery实现$.fn.extend和$.extend函数
2016/04/14 Javascript
js点击返回跳转到指定页面实现过程
2020/08/20 Javascript
浅谈JS读取DOM对象(标签)的自定义属性
2016/11/21 Javascript
jQuery密码强度验证控件使用详解
2017/01/05 Javascript
angular4模块中给标签添加背景图的实现方法
2017/09/15 Javascript
原生JS实现的简单小钟表功能示例
2018/08/30 Javascript
Vue源码之关于vm.$delete()/Vue.use()内部原理详解
2019/05/01 Javascript
Flask之flask-script模块使用
2018/07/26 Python
python制作填词游戏步骤详解
2019/05/05 Python
Python函数和模块的使用总结
2019/05/20 Python
pandas DataFrame 警告(SettingWithCopyWarning)的解决
2019/07/23 Python
python使用Thread的setDaemon启动后台线程教程
2020/04/25 Python
如何配置关联Python 解释器 Anaconda的教程(图解)
2020/04/30 Python
PyQt5实现简单的计算器
2020/05/30 Python
使用OpenCV获取图片连通域数量,并用不同颜色标记函
2020/06/04 Python
python文件排序的方法总结
2020/09/13 Python
瑞典轮胎在线:Tirendo.se
2018/06/21 全球购物
香港彩色隐形眼镜在线商店:Stunninglens(全球免费送货)
2019/05/10 全球购物
美国帽子俱乐部商店:Hat Club
2019/07/05 全球购物
美国领先的机场停车聚合商:Airport Parking Reservations
2020/02/28 全球购物
2013的个人自我评价
2013/12/26 职场文书
打架检讨书800字
2014/01/10 职场文书
电气自动化专业职业规划范文
2014/02/16 职场文书
大学生党员学习焦裕禄精神思想汇报
2014/09/10 职场文书
见习期个人总结
2015/03/05 职场文书
毕业欢送会致辞
2015/07/29 职场文书
Go语言测试库testify使用学习
2022/07/23 Golang