javascript - 怎么使用正则判断input的值?

查看:100
本文介绍了javascript - 怎么使用正则判断input的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<input id="dialogPrice" type="text" required pattern="/\D/g,''" class="weui-input" placeholder="¥10" />

要在input里验证输入的数字,只能输入正整数,整数小于200,不能是0或空格或汉字,要怎么写?

我现在写的是:

<input id="dialogPrice" type="text" required class="weui-input" placeholder="¥10" onkeyup= "if(! /^d+$/.test(this.value)){weui.topTips('只能整数');}"/>

js:

$('#otherPriceBtn').on('click', function(e) {
                    var otherPrice = $('#dialogPrice').val();  
                    otherPrice = parseInt(otherPrice);
                    
                    IsNull(otherPrice);
                    isZero(otherPrice);
                    isChinese(otherPrice);
                    isMax(otherPrice);
}

然后写了四个方法:

//判断输入内容是否为空    
                function IsNull(value){    
                    if(value.length==0){    
                        weui.topTips('对不起,不能为空或者为空格!');
                    }else{
                        return '';
                    }
                }
                //判断输入内容不能为0
                function isZero(value){
                    if(value == 0){
                        weui.topTips('对不起,金额不可为0');
                    }else{
                        return '';
                    }
                }
                //判断不能输入汉字
                function isChinese(value){
                    if(value.length != 0){
                        reg=/^[\u0391-\uFFE5]+$/;
                        if(!reg.test(value)){
                            weui.topTips('对不起,金额不可为汉字');
                        }else{
                            return '';
                        }
                    }
                }
                //判断最大值不能超过200
                function isMax(value){
                    if(value.length != 0){
                        reg=/^[-+]?\d*$/;
                        //判断是否为数字类型
                        if(!reg.test(value)){
                            if(value > parseInt(200)){
                                weui.topTips('对不起,金额不可大于200');
                            }
                        }
                    }
                }

但是不好使,如果分别提示不能是汉字、不能有空格、不能是0、整数必须小于200要怎么写?谢谢

解决方案

用了笨办法,解决了,能用倒是能用,就是有点丑:

//验证
            $('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();
                }
            })

这篇关于javascript - 怎么使用正则判断input的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆