<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要怎么写?谢谢
用了笨办法,解决了,能用倒是能用,就是有点丑: