急求一个在网页中验证用户输入的JavaScript代码 比如有一个textbox,用户输入后点button按钮时,实现用JavaScript对textbox输入信息的验证,比如验证用户输入的是否是数字。希望将HTML代码一并写出来哈,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>无标题页 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript"> window.onload = function(){ document.getElementById('click').onclick = function(){ if(!isNaN(document.getElementById('text').value)){ alert('是数字'); }else{ alert('不是数字'); } }; };</script></head> <body> <input type="text" id="text" /> <input type="button" id="click" value="click" /></body> </html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 加上这句要么就改成gb2312 改成gb2312此问题已解决。但是如果用户输入的是数字,在用户点击button按钮时,我就将其跳转到B.html页面的话,对于楼上给出的代码该如何在代码中实现? window.onload = function(){ document.getElementById('click').onclick = function(){ if(!isNaN(document.getElementById('text').value)){ window.open('B.html') }else{ alert('不是数字'); } }; }; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>无标题页 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript"> window.onload = function(){ document.getElementById('click').onclick = function(){ if(!isNaN(document.getElementById('text').value)){ window.location = 'B.html'; }else{ alert('不是数字'); } }; };</script></head> <body> <input type="text" id="text" /> <input type="button" id="click" value="click" /></body> </html> 谢谢各位的回答。对于上面的代码,如果用正则表达式怎么实现呢?(也就是不用isNaN这个函数,而是用正则表达式来实现) re=/(d+)/g if(re.test(document.getElementById('text').value)){ alert('数字')}else{ alert('不是数字')} 下面是一个正则表达式的写法 希望能对你有所帮助<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script language="javascript" type="text/javascript"> function test(){ var reg = /^[\d]{5}$/; <!--正则表达式声明只能输入0-9之间的数字并且必须输入的是5个数字才是匹配的--> var text = document.getElementById('txt').value; if(reg.test(text)){ alert('匹配!'); }else{ alert('不匹配!'); } }</script></head><body><input type="text" id="txt"/><input type="button" value="submit" onclick="test()"/></body></html> if(!/^\d*$/.test(txt.value)){ alert('不是数字')} 如何让整数也保留2位小数(678变成678.00) 请高手支招!字符型数字数字比较大小 js删除对象的问题! 寻求好的处理方法 为啥明明定义了位置,用style.left取不出 检测是否已安装 .NET Framework 时钟setInterval("getparam();",2000);来定时更新,为什么不能更新呢? CSDN左边选择论谈菜单的代码如何写? 字符和数字转换以及字符串分割函数split()的问题 紧急!!!! 怎么在el-table单元格内加图标? 如何设定鼠标在控件上停2秒就显示信息 一个很怪异的问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){
document.getElementById('click').onclick = function(){
if(!isNaN(document.getElementById('text').value)){
alert('是数字');
}else{
alert('不是数字');
}
};
};
</script>
</head>
<body>
<input type="text" id="text" />
<input type="button" id="click" value="click" />
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 加上这句要么就改成gb2312
但是如果用户输入的是数字,在用户点击button按钮时,我就将其跳转到B.html页面的话,对于楼上给出的代码该如何在代码中实现?
document.getElementById('click').onclick = function(){
if(!isNaN(document.getElementById('text').value)){
window.open('B.html')
}else{
alert('不是数字');
}
};
};
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){
document.getElementById('click').onclick = function(){
if(!isNaN(document.getElementById('text').value)){
window.location = 'B.html';
}else{
alert('不是数字');
}
};
};
</script>
</head>
<body>
<input type="text" id="text" />
<input type="button" id="click" value="click" />
</body>
</html>
对于上面的代码,如果用正则表达式怎么实现呢?(也就是不用isNaN这个函数,而是用正则表达式来实现)
re=/(d+)/g
if(re.test(document.getElementById('text').value)){
alert('数字')
}
else{
alert('不是数字')
}
希望能对你有所帮助
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function test(){
var reg = /^[\d]{5}$/; <!--正则表达式声明只能输入0-9之间的数字并且必须输入的是5个数字才是匹配的-->
var text = document.getElementById('txt').value;
if(reg.test(text)){
alert('匹配!');
}else{
alert('不匹配!');
}
}
</script>
</head>
<body>
<input type="text" id="txt"/>
<input type="button" value="submit" onclick="test()"/>
</body>
</html>
alert('不是数字')
}