巧用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 相关文章推荐
解析javascript 浏览器关闭事件
Jul 08 Javascript
jQuery复制表单元素附源码分享效果演示
Sep 30 Javascript
JavaScript统计网站访问次数的实现代码
Nov 18 Javascript
第九章之路径分页标签与徽章组件
Apr 25 Javascript
详解js运算符单竖杠“|”与“||”的用法和作用介绍
Nov 04 Javascript
bootstrap实现动态进度条效果
Mar 08 Javascript
jQuery实现的动态文字变化输出效果示例【附演示与demo源码下载】
Mar 24 jQuery
详解Vue使用命令行搭建单页面应用
May 24 Javascript
Layui给数据表格动态添加一行并跳转到添加行所在页的方法
Aug 20 Javascript
vue-cli3.0使用及部分配置详解
Aug 29 Javascript
可能被忽略的一些JavaScript数组方法细节
Feb 28 Javascript
layui点击数据表格添加或删除一行的例子
Sep 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 采集程序 常用函数
2008/12/18 PHP
php strstr查找字符串中是否包含某些字符的查找函数
2010/06/03 PHP
PHP类继承 extends使用介绍
2014/01/14 PHP
Php无限级栏目分类读取的实现代码
2014/02/19 PHP
php实现随机显示图片方法汇总
2015/05/21 PHP
PHP的介绍以及优势详细分析
2019/09/05 PHP
JS清除字符串中重复值的实现方法
2016/08/03 Javascript
jQuery实现表格与ckeckbox的全选与单选功能
2016/11/24 Javascript
Javascript之面向对象--封装
2016/12/02 Javascript
创建简单的node服务器实例(分享)
2017/06/23 Javascript
简单谈谈axios中的get,post方法
2017/06/25 Javascript
VUE饿了么树形控件添加增删改功能的示例代码
2017/10/17 Javascript
详解webpack模块加载器兼打包工具
2018/09/11 Javascript
vue data对象重新赋值无效(未更改)的解决方式
2020/07/24 Javascript
vue中touch和click共存的解决方式
2020/07/28 Javascript
解决vue net :ERR_CONNECTION_REFUSED报错问题
2020/08/13 Javascript
[15:58]DOTA2国际邀请赛采访专栏:Tongfu.Sansheng&KingJ,DK.rOtk
2013/08/08 DOTA
[02:28]DOTA2英雄基础教程 狼人
2013/12/23 DOTA
[01:21:58]守擂赛DOTA2第一周决赛
2020/04/22 DOTA
Python语言实现获取主机名根据端口杀死进程
2016/03/31 Python
Python深入06——python的内存管理详解
2016/12/07 Python
python绘制双柱形图代码实例
2017/12/14 Python
Python装饰器用法实例总结
2018/02/07 Python
python调用Matplotlib绘制分布点并且添加标签
2018/05/31 Python
python实现汽车管理系统
2018/11/30 Python
Django 1.10以上版本 url 配置注意事项详解
2019/08/05 Python
pygame编写音乐播放器的实现代码示例
2019/11/19 Python
Python:slice与indices的用法
2019/11/25 Python
python装饰器的特性原理详解
2019/12/25 Python
python 对象真假值的实例(哪些视为False)
2020/12/11 Python
欧舒丹英国官网:购买欧舒丹护手霜等明星产品
2017/01/17 全球购物
奥斯汀独木舟和皮划艇:Austin Canoe & Kayak
2018/05/22 全球购物
失业者真诚求职信范文
2013/12/25 职场文书
公休请假条
2014/04/11 职场文书
工伤事故证明
2014/10/20 职场文书
离婚协议书范本(2016最新版)
2016/03/18 职场文书