当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 巧用weui.topTips验证数据的实例

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

2018年05月21日  | 移动技术网IT编程  | 我要评论

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

不能为空格;

不能为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验证数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网