課程
/前端開發
/JavaScript
/JavaScript進階篇
怎么判斷輸入的是不是數字?不是數字能報錯
2016-11-04
源自:JavaScript進階篇 5-6
正在回答
typeof(那個輸入的東西);
__正正經經先生4337661 提問者
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>返回值函數</title>
<script type="text/javascript">
var arr=['as',100,'xx'-6,'100px']
document.write("1.數組中是純數字的有:") ? //100
for(var i=0 ; i<arr.length ;i++)
{
//isNaN()很特別如果你是純數字會判斷為false;其他就為true;
if(isNaN(arr[i])===false)
{ document.write(arr[i]) }
}
document.write("</br>"+"2.數組中不是純數字的有:") //'as','xx'-6,'100px'
for(var n = 0 ; n < arr.length ;n++)
//當 isNaN()判斷出 你不是純數字的同時又屬于數字類型的時候(就是含有數字的字符串),輸出值為NaN
if(isNaN(arr[n]) === true)
{ document.write(arr[n]+",") ?}
document.write("</br>"+"3.數組中是純數字的有:") ? //100
for(var p=0 ; p<arr.length ;p++)
//parseInt()用于強轉為數字類型,比如parseInt('100px')=100 ;parseInt('560ss')=560
if(parseInt(arr[p]) === arr[p])
{ document.write(arr[p]) }
//轉換條件自己推吧
console.log("parseInt('100px')="+parseInt('100px'))
console.log("parseInt('a')="+parseInt('a'))
console.log("parseInt('10a10')="+parseInt('10a10'))
console.log("parseInt('a10')="+parseInt('a10'))
document.write("</br>"+"4.數組中屬于數字類型的有:")//100,'xx'-6
for(var j = 0 ; j < arr.length ;j++)
//typeof用于判斷字符串類型
if(typeof arr[j] === "number")
{ document.write(arr[j]+",") }
//還搞懂isNaN()就對比一下1,2,4的關系把
</script>
</head>
<body>
</body>
</html>
慕粉3912077
var a='a';
a=a*1;
console.log(isNaN(a));//true
var b='1';
b=b*1;
console.log(isNaN(b));//false
比如說一個輸入框,你輸入了一個東西,然后點擊一個button,這個時候判斷的輸入的是不是數字肯定不能直接判斷,因為你輸入進去取出來的都是string,這個時候你需要乘以1,獲取這個乘以1的值,用isNaN進行判斷,如果你輸入的不是數字判斷結果都是true,輸入的是數字判斷結果都是false;
慕粉4259319 回復 __正正經經先生4337661 提問者
__正正經經先生4337661 提問者 回復 慕粉4259319
if(isNaN(otext.value) === true) ? ?
? ?文本.innerHTML="輸入的不是數字" ? ?
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
1 回答在計算器功能中,怎么判斷用戶輸入的是數字而不是字母或者其他?
4 回答字符串是怎么判斷排序的???看不懂。
2 回答在任務三中,我怎么判斷用戶輸入的是數字而不是字母abc;例如用戶輸入的是12ab3,我應該如何跳過ab,將123復選框勾上?
1 回答怎樣判斷是二維數組
2 回答輸入合法性判斷
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-11-04
typeof(那個輸入的東西);
2016-11-04
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>返回值函數</title>
<script type="text/javascript">
var arr=['as',100,'xx'-6,'100px']
document.write("1.數組中是純數字的有:") ? //100
for(var i=0 ; i<arr.length ;i++)
{
//isNaN()很特別如果你是純數字會判斷為false;其他就為true;
if(isNaN(arr[i])===false)
{ document.write(arr[i]) }
}
document.write("</br>"+"2.數組中不是純數字的有:") //'as','xx'-6,'100px'
for(var n = 0 ; n < arr.length ;n++)
{
//當 isNaN()判斷出 你不是純數字的同時又屬于數字類型的時候(就是含有數字的字符串),輸出值為NaN
if(isNaN(arr[n]) === true)
{ document.write(arr[n]+",") ?}
}
document.write("</br>"+"3.數組中是純數字的有:") ? //100
for(var p=0 ; p<arr.length ;p++)
{
//parseInt()用于強轉為數字類型,比如parseInt('100px')=100 ;parseInt('560ss')=560
if(parseInt(arr[p]) === arr[p])
{ document.write(arr[p]) }
}
//轉換條件自己推吧
console.log("parseInt('100px')="+parseInt('100px'))
console.log("parseInt('a')="+parseInt('a'))
console.log("parseInt('10a10')="+parseInt('10a10'))
console.log("parseInt('a10')="+parseInt('a10'))
document.write("</br>"+"4.數組中屬于數字類型的有:")//100,'xx'-6
for(var j = 0 ; j < arr.length ;j++)
{
//typeof用于判斷字符串類型
if(typeof arr[j] === "number")
{ document.write(arr[j]+",") }
//還搞懂isNaN()就對比一下1,2,4的關系把
}
</script>
</head>
<body>
</body>
</html>
2016-11-04
var a='a';
a=a*1;
console.log(isNaN(a));//true
var b='1';
b=b*1;
console.log(isNaN(b));//false
比如說一個輸入框,你輸入了一個東西,然后點擊一個button,這個時候判斷的輸入的是不是數字肯定不能直接判斷,因為你輸入進去取出來的都是string,這個時候你需要乘以1,獲取這個乘以1的值,用isNaN進行判斷,如果你輸入的不是數字判斷結果都是true,輸入的是數字判斷結果都是false;
2016-11-04
if(isNaN(otext.value) === true) ? ?
? ?文本.innerHTML="輸入的不是數字" ? ?