2017-04-13 10 views
0

誰かが以下のコードがうまく動作しない可能性がある理由を教えてもらえますか?ボタンがヒットしたとき、ユーザーが入力フィールドに数字を入力したかどうかに基づいて、正しいかどうかを表示したい。isNaNが正しく動作していないようです

HTML:

<!DOCTYPE html> 
<body> 
<p>Enter name:</p> 
<input type="text" name="name"></input> 
<button onclick="checkpeople()" type = "button">check</button><br> 
<p id="message"></p> 
<script src="script.js"></script> 
</body> 

JS:

function checkpeople() { 
var name = ""; 
if (isNaN(name)) { 
name = "incorrect" 
} 
else { 
    name = "correct" 
} 
document.getElementById('message').innerHTML = name; 
} 
+1

あなたが 'ますisNaN(に渡す引数をあなたの関数は常に正しい ""を表示します。入力の '.value'プロパティを使う以外は、出力要素の' .innerHTML'をどのように設定するのかと同様に、入力から値を得ることができます。 (そして、入力から値を取得するようにコードを変更したら、 'isNaN()'は空の文字列を渡すと 'false'を返します) – nnnnnn

答えて

0

isNAN(<argument>)与えられた引数が不正な番号であるかどうかを伝えるための機能です。 isNaNは引数をNumber型にキャストします。引数が数値かどうかをチェックしたいのですか? jQuery$.isNumeric()の機能を使用してください。

つまり、isNaN(foo)はisNaN(Number(foo))と等価です。明らかな理由から、すべての数字を数字として持つ文字列を受け入れます。例えば、

isNaN(123) //false 
isNaN(-1.23) //false 
isNaN(5-2) //false 
isNaN(0) //false 
isNaN('123') //false 
isNaN('Hello') //true 
isNaN('2005/12/12') //true 
isNaN('') //false 
isNaN(true) //false 
isNaN(undefined) //true 
isNaN('NaN') //true 
isNaN(NaN) //true 
isNaN(0/0) //true 

jQuery.isNumeric():https://api.jquery.com/jQuery.isNumeric/ あなたはjQueryのを使用しないことについて独特であり、JavaScriptでのみ、それをdoint場合。あなたは次のように使用することができます:isNan(parseInt(<argument>))または

isNumber = function(obj){ 
 
    
 

 
    
 

 
    if (!isNaN(parseInt(obj))) { 
 
    // Is a number 
 
    return true; 
 
} 
 
else return false; 
 
} 
 

 
var num = 14; 
 
var textnum = '14'; 
 
var text = 'yo'; 
 
var nan = NaN; 
 

 
var DOM = document.getElementById("demo").innerHTML; 
 
document.getElementById("demo").innerHTML = isNumber(textnum, true);
<p id="demo"></p>

+1

*"フレームワーク/ライブラリも含まれていますが、純粋なJavaScriptの答えが必要です "* - " javascript "タグ情報から。しかし、いずれにしても、isNaN()関数の動作は、OPのコードの主な問題ではありません。 – nnnnnn

1

問題がcheckPeople関数内の変数nameがあなたのhtmlでinputタグには一切リンクされていないということですisNan(parseFLoat(<argument>))checkPeople関数で入力タグの値を取得する必要があります。データが数値でない場合

function checkpeople() { 
 
var name = document.getElementById('myinput').value; 
 
if (isNaN(name)) { 
 
name = "incorrect" 
 
} 
 
else { 
 
    name = "correct" 
 
} 
 
document.getElementById('message').innerHTML = name; 
 
}
<!DOCTYPE html> 
 
<body> 
 
<p>Enter name:</p> 
 
<input type="text" name="name" id="myinput"></input> 
 
<button onclick="checkpeople()" type = "button">check</button><br> 
 
<p id="message"></p> 
 
</body>

+0

私はまた、間違いが間違っていなければならないと考えています。私はその意図が数字のために間違っていることを意図しています。 asNaNは 'correct'となるテキストに対してtrueを返します。 – andrewf

2

ますisNaN()がtrueを返す:私は、以下の作業のコードを提供してきました。 is Not a NumberのためのすべてのisNaNスタンドの

function checkpeople() { 
 
var name = document.getElementById('myinput').value; 
 
if (isNaN(name)) { 
 
name = "correct" 
 
} 
 
else { 
 
    name = "incorrect" 
 
} 
 
document.getElementById('message').innerHTML = name; 
 
}
<!DOCTYPE html> 
 
<body> 
 
<p>Enter name:</p> 
 
<input type="text" name="name" id="myinput"></input> 
 
<button onclick="checkpeople()" type = "button">check</button><br> 
 
<p id="message"></p> 
 
</body>

0

まず、そのため、

isNaN(12234) //will be false 

isNaN("jsjsjw")// will return true. 

ながら、あなたのコード内の条件が逆転しなければならないので。つまり、あなたの名前が数字でないようにしたいのです。

2番目にコードが入力を接続していないため、jsでチェックする前に入力から値を取得する必要があります。

function checkpeople() { 
var name = document.getElementById('myinput').value; 
if (isNaN(name)) { 
    name = "correct" 
} 
else { 
    name = "incorrect" 
} 
document.getElementById('message').innerHTML = name; 
0

使用isInteger。 isNaNよりもはるかに優れています。

Number.isInteger(0);   // true 
Number.isInteger(1);   // true 
Number.isInteger('10');  // false 

some problem with usingisNaNです。

document.write(isNaN("")) // false 
document.write(isNaN(" ")) // false 
document.write(isNaN(0)) // false 
document.write(isNaN(null)) // false 
document.write(isNaN(false)) // false 
document.write("" == false) // true 
document.write("" == 0) // true 
document.write(" " == 0) // true 
document.write(" " == false) // true 
document.write(0 == false) // true 
document.write(" " == "") // false 

インターネットエクスプローラのサポートが必要な場合は、polyfillsを使用できます。

可能な限りネイティブJavaScriptを使用してください。あなたは、他のフレームワークとの依存性も低く、JavaScriptを使って多くのことを学ぶことができます。

0

テキストボックスから入力した値を選択していないため、単純に空の文字列を比較しています。以下は

var name = ""; 
if (isNaN(name)) { 

例取り組んでいる: `*ない*、常に` name`の変数から空の文字列です)

function checkpeople() { 
 
    // changed variable name, as there is global variable already defined i.e. `window.name` 
 
    var name_ = document.getElementsByName("name")[0].value; 
 
    if (!name_ || isNaN(name_)) { // if not empty and not a number 
 
    name_ = "incorrect" 
 
    } else { 
 
    name_ = "correct" 
 
    } 
 
    document.getElementById('message').innerHTML = name_; 
 
}
<p>Enter name:</p> 
 
<input type="text" name="name" /> 
 
<button onclick="checkpeople()" type="button">check</button><br> 
 
<p id="message"></p>

関連する問題