javascript - 怎么使用正则判断input的值?
高洛峰
高洛峰 2017-04-11 11:30:53
[JavaScript讨论组]

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

我现在写的是:

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要怎么写?谢谢

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
ringa_lee

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

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

如果是用HTML5做表单验证的话,像下面这样就可以了:

    <form>
        <input type="number" name="" min="1" max="199"  required >
        <button type="submit">submit </button>
    </form>
巴扎黑

你需要 jquery.validate

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号