js判断文本框里面是否有数字,如果为空。则弹出提示。要求重新输入
html页面
- HTML code
<form onsubmit="return haoyoujisuan(this)"> <table width="100%" cellspacing="1" cellpadding="8" border="0"> <tr> <td width="35%" align="center">上次加满油后行驶的公里数(A):</td><td width="65%" ><input type="text" maxlength="20" size="20" value="" onkeyup="value=value.replace(/[^\d|.]/g,'')" id="a" name="a"> 公里</td></tr> <tr> <td align="center">本次油箱加满公升数(B):</td> <td ><input type="text" maxlength="20" size="20" value="" onkeyup="value=value.replace(/[^\d|.]/g,'')" id="b" name="b"> 公升</td> </tr> <tr> <td align="center">当日油价 (C):</td> <td ><input type="text" maxlength="20" size="20" value="" onkeyup="value=value.replace(/[^\d|.]/g,'')" id="c" name="c"> 元/ 公升</td> </tr> <tr> <td align="right" colspan="2"><input type="submit" value="进行计算 >>"></td> </tr></table>
js代码如下:
function haoyoujisuan(f)
{
var obaoil =f.a.value;
var obboil =f.b.value;
var obcoil =f.c.value;
rs1.innerHTML = (((Math.floor((Math.floor(obaoil/obaoil*1000)*1000)))/1000)/1000)+"公里";
rs2.innerHTML = (((Math.floor((Math.floor(obboil/obaoil*1000)*1000)))/1000)/1000)+"公升(约等同"+(((Math.floor((Math.floor(obboil/obaoil*1000*1000)*1000)))/1000)/1000)+"cc)的油";
rs3.innerHTML = (((Math.floor((Math.floor(obboil/obaoil*obcoil*1000)*1000)))/1000)/1000)+"元";
return false;
}
------解决方案--------------------
1.
<form name="f" id="f" ....>
2.
- JScript code
<script> function haoyoujisuan(f) { var aa =f.a.value; var bb=f.b.value; var cc=f.c.value; if(aa.length==0){ alert('公里数不允许为空值'); f.a.focus(); return false; } if(bb.length==0){ alert('公升数不允许为空值'); f.b.focus(); return false; } if(cc.length==0){ alert('当日油价不允许为空值'); f.b.focus(); return false; } } </script>
------解决方案--------------------
------解决方案--------------------
这个JS验证数字、邮箱、电话等等有正则表达式
还有验证不能为空、只能输入数字等等这些验证可以提一个公共的方法的,没必要每一个控件验证都写一个方法
比如:
- JScript code
//必录项验证 function checkRequered(obj,name) { if(obj == null || obj.value=="") { alert(name+" 不能为空,请输入内容!"); obj.focus(); return false; } return true; }
------解决方案--------------------
------解决方案--------------------