全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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

不能为空格;

不能为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>

场景二、所有违反规距的都有信息提示,但是“确定”按钮不隐藏,只是删除它的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验证数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


# weui  # toptips  # weUI应用之JS常用信息提示弹层的封装  # Angularjs整合微信UI(weui)  # weui框架实现上传、预览和删除图片功能代码  # 为空  # 能为  # 自定义  # 重新输入  # 都是  # 给大家  # 的是  # 特殊字符  # 都有  # 输入汉字  # 才会  # 希望能  # 才可以  # 为其  # 跳转  # 这篇  # 小编  # 符合条件  # 大家多多  # 巧用 


相关文章: 香港服务器网站推广:SEO优化与外贸独立站搭建策略  网站制作企业,网站的banner和导航栏是指什么?  大学网站设计制作软件有哪些,如何将网站制作成自己app?  如何获取免费开源的自助建站系统源码?  怎么将XML数据可视化 D3.js加载XML  黑客如何利用漏洞与弱口令入侵网站服务器?  再谈Python中的字符串与字符编码(推荐)  深圳网站制作案例,网页的相关名词有哪些?  深圳网站制作平台,深圳市做网站好的公司有哪些?  成都网站制作价格表,现在成都广电的单独网络宽带有多少的,资费是什么情况呢?  建站之星×万网:智能建站系统+自助建站平台一键生成  电商平台网站制作流程,电商网站如何制作?  网站制作需要会哪些技术,建立一个网站要花费多少?  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  如何通过智能用户系统一键生成高效建站方案?  如何在建站宝盒中设置产品搜索功能?  商务网站制作工程师,从哪几个方面把握电子商务网站主页和页面的特色设计?  如何在香港免费服务器上快速搭建网站?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  孙琪峥织梦建站教程如何优化数据库安全?  建站之星在线客服如何快速接入解答?  如何快速查询域名建站关键信息?  天河区网站制作公司,广州天河区如何办理身份证?需要什么资料有预约的网站吗?  建站主机空间推荐 高性价比配置与快速部署方案解析  如何打造高效商业网站?建站目的决定转化率  已有域名建站全流程解析:网站搭建步骤与建站工具选择  网站制作新手教程,新手建设一个网站需要注意些什么?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  如何用花生壳三步快速搭建专属网站?  网站制作说明怎么写,简述网页设计的流程并说明原因?  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  建站之星如何保障用户数据免受黑客入侵?  实现虚拟支付需哪些建站技术支撑?  如何制作算命网站,怎么注册算命网站?  建站之星备案流程有哪些注意事项?  岳西云建站教程与模板下载_一站式快速建站系统操作指南  如何通过虚拟主机空间快速建站?  如何彻底卸载建站之星软件?  手机网站制作与建设方案,手机网站如何建设?  c# Task.ConfigureAwait(true) 在什么场景下是必须的  大连 网站制作,大连天途有线官网?  网站制作软件免费下载安装,有哪些免费下载的软件网站?  全景视频制作网站有哪些,全景图怎么做成网页?  零基础网站服务器架设实战:轻量应用与域名解析配置指南  建站之星收费标准详解:套餐费用及年费价格表一览  如何通过可视化优化提升建站效果?  宝塔新建站点报错如何解决?  如何在建站主机中优化服务器配置?  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  如何在腾讯云服务器上快速搭建个人网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。