如何判断用户输入的是否为合法的! javascript:isNaN()jsp:try{Doule.parseDouble(request.getParameter("fieldname"));//是数字}catch(Exception e){//不是数字} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{Doule.parseDouble(jTextArea1.getText());//是数字}catch(Exception e){//不是数字} 如果是在网页中可以使用JavaScript判断各个字符的ASCII码来确定是否输入的为数字。function isMoney(){ var str=form1.card_value.value; var c; var length=str.length; if(length>0) { var c=str.charAt(0); if(parseInt(c)==0) return false; for(var i=0;i<length;c=str.charAt(++i)) if(isNaN(parseInt(c)))return false; } return true;} <script language="JavaScript"><!--function Check(form){temp=form.ara.value;for(i=0;i<temp.length;i++){ tempSub=temp.substring(i,i+1); if(tempSub>=0&&tempSub<=9){} else{ window.alert("ara输入值必须为数字!"); document.form.RASPort.focus(); NotNull=false; break; } } return NotNull;}//--></script><form name="form" method="post" action="wt.jsp" onSubmit="return Check(this)"><input name="ara" type="text"> 忘了声明了 var NotNull; var temp; var tempSub; NotNull=true;:) //==============是否只由数字组成的字符串len=20================ function isNumber(str) { var re=/^[0-9]{1,20}$/; if ( re.test( str ) ) { return true; } else { return false; } } 我说的是JavaScript,你可以改字符串长度,就改一下那个,20就行了。 如果你只要数字的话,干脆限制用户输入好了。用正则表达式:只能输入数字的:<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"> 如何html跳转action中的一个方法 求助,java.sql.SQLException:流已被关闭 freemarker+struts2 jsp 页面遍历List<Map<Long,List<Boxindaga>>> struts或者jstl 救命 搞死我了 javascript然后滑出小窗口 Web引擎可以自动关闭线程吗? jsp中的重定向问题?? 请教各位DX一个很棘手的问题,望各位不惜赐教!! 100分求解 为什么只是显示数据库中的第一条记录啊?? 读文件时得权限问题 用javabean有什么好处呢?在线等待!!! 请教大侠们
}catch(Exception e){//不是数字}
{
var str=form1.card_value.value;
var c;
var length=str.length;
if(length>0)
{
var c=str.charAt(0);
if(parseInt(c)==0)
return false;
for(var i=0;i<length;c=str.charAt(++i))
if(isNaN(parseInt(c)))return false;
}
return true;
}
<!--
function Check(form)
{
temp=form.ara.value;
for(i=0;i<temp.length;i++){
tempSub=temp.substring(i,i+1);
if(tempSub>=0&&tempSub<=9){}
else{
window.alert("ara输入值必须为数字!");
document.form.RASPort.focus();
NotNull=false;
break;
}
}
return NotNull;
}
//-->
</script>
<form name="form" method="post" action="wt.jsp" onSubmit="return Check(this)">
<input name="ara" type="text">
var temp;
var tempSub;
NotNull=true;
:)
function isNumber(str)
{
var re=/^[0-9]{1,20}$/;
if ( re.test( str ) )
{
return true;
}
else
{
return false;
} }
用正则表达式:
只能输入数字的:
<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">