[size=large][url]http://chengxuyuanbiji.blog.163.com/blog/static/1824565062011444832369/[/url]
<!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=gb2312" />
<script>
function check(obj){
if(obj.value!=""){
if(isNaN(obj.value)){ // isNaN -->is not a number 判断是不是数字 是返回true 不是返回false
alert("请填写数字!");
obj.focus();
obj.select();
window.setTimeout( function(){ obj.focus();obj.select(); }, 0); //为了兼容火狐加的
}
}
}
</script>
<input type="input" onblur="check(this)"/>
</body>
</html>
[/size]
<!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=gb2312" />
<script>
function check(obj){
if(obj.value!=""){
if(isNaN(obj.value)){ // isNaN -->is not a number 判断是不是数字 是返回true 不是返回false
alert("请填写数字!");
obj.focus();
obj.select();
window.setTimeout( function(){ obj.focus();obj.select(); }, 0); //为了兼容火狐加的
}
}
}
</script>
<input type="input" onblur="check(this)"/>
</body>
</html>
[/size]

本文介绍了一段用于检查输入是否为数字的JavaScript代码片段,实现了基本的输入验证功能,并在非数字输入时提供了错误提示。
2102

被折叠的 条评论
为什么被折叠?



