2017-02-28 120 views
1

我在HTML頁面上工作,似乎無法弄清楚如何檢查用戶輸入是否有一個整數。我已經在for循環中嘗試了parseInt,userInput.charAt(i),isNaN。似乎沒有任何工作。檢查輸入是否有int int Javascript

這是我的HTML

<label for="author" id="author">Favorite Book Author: </label> 
<input type="text" name="author" id="authorText"> 

這是我的JavaScript

function checkAuthor(){ 
    var userInput = $("authorText").value; 
    for(var i = 0; i < userInput.length; i++) 
    { 
     if(userInput.charAt(i) <= 0 || userInput.charAt(i) > 0) 
     { 
      addError("There is a number in 'Favorite Book Author' input"); 
      return false; 
     } 
     else 
      return true; 
    } 
} 

我要看看是否有是一個由用戶輸入任意整數。如果輸入是「Mark5」,「Ma5rk」,「5」,我需要它來捕捉5並顯示錯誤。反正會有效嗎?我有一個在.charAt不工作,因爲它認識到整數爲字符串的感覺,但我試圖做的每一個

var x = parseInt(userInput.charAt(i)); 

一個parseInt函數內的循環,則x裏面的,如果比較聲明。但似乎沒有任何工作

的addError功能是本

function addError(text){ 
    var mylist = window.document.createElement("li"); 
    var myText = window.document.createTextNode(text); 
    mylist.appendChild(myText); 
    window.document.getElementByTagName("ul")[0].appendChild(mylist); 
} 
+0

你應該使用正則表達式 – mvermand

+0

在讀取輸入值你有一個錯字錯。使用$(「#authorText」)。value; –

+0

這是可能的,但你只關心數字嗎?其他非字母輸入存在 –

回答

0

您應該檢查字符碼48和57之間

function checkAuthor(){ 
 
    var userInput = $("authorText").value; 
 
    for(var i = 0; i < userInput.length; i++) 
 
    { 
 
     var c = userInput.charAt(i).charCodeAt(0); 
 
     if(c <= 57 && c >= 48) 
 
     { 
 
      addError("There is a number in 'Favorite Book Author' input"); 
 
      return false; 
 
     } 
 
      
 
    } 
 
    return true; 
 

 
}

+0

正則表達式適用於像這樣的場景。那麼爲什麼你把代碼中的超載? –

+0

我收到一個錯誤「Uncaught ReferenceError:charCodeAt is not defined」當我試過這個 – Programmer

+0

好吧我只是修復這個函數,請重試 –

0

您可以使用正則表達式來搜索。

var userInput = $("authorText").value; 
if (userInput.search(/[^a-zA-Z]+/)) { 
    addError("There is a number in 'Favorite Book Author' input"); 
return false. 
} 
+0

由於某些原因,即使我在HTML中輸入了一個沒有整數的字符串,它仍然輸入了if語句 – Programmer

+0

您是否可以在您使用過正則表達式的地方分享完整的代碼塊? –

+0

什麼是正則表達式?從未聽說過這個詞 – Programmer

0

的jQuery和JavaScript是否不一樣的。
您不能在返回的$()中使用value。改爲使用val()並使用#將ID用作選擇器。 使用[0-9]來檢查數字是否存在。

function checkAuthor(){ 
 
    var userInput = $("#authorText").val(); // add # to get ID and val() 
 
    var result = /[0-9]+/.test(userInput); 
 
    result?alert("false"):void(0); 
 
    console.clear(); 
 
    console.log(!result); 
 
    return !result; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label for="author" id="author">Favorite Book Author: </label> 
 
<input type="text" name="author" id="authorText" onblur="checkAuthor();" />

0

由於您尋找單號,RejExp測試將可能是最簡單的/清潔的方法。

JQuery的

function checkAuthor(){ 
    var userInput = $("#authorText").val(); 

    // check whether any char in userInput is a number. yes: true. no: false 
    if(/[0-9]/.test(userInput)) { 
     addError("There is a number in 'Favorite Book Author' input"); 
     return false; 
    } else 
     return true; 
    } 
} 

function addError(text){ 
    var mylist = window.document.createElement("li"); 
    var myText = window.document.createTextNode(text); 
    mylist.appendChild(myText); 
    window.document.getElementByTagName("ul")[0].appendChild(mylist); 
} 

香草JS

function checkAuthor(){ 
    var userInput = document.getElementById('#authorText').value; 

    // check whether any char in userInput is a number. yes: true. no: false 
    if(/[0-9]/.test(userInput)) { 
     addError("There is a number in 'Favorite Book Author' input"); 
     return false; 
    } else 
     return true; 
    } 
} 

function addError(text){ 
    var mylist = window.document.createElement("li"); 
    var myText = window.document.createTextNode(text); 
    mylist.appendChild(myText); 
    window.document.getElementByTagName("ul")[0].appendChild(mylist); 
} 
+0

。價值?你在jQuery中的意思是什麼? –

+0

也定義'addError' –

+0

感謝.val(),我實際上在編輯我的消息後看到了這些註釋。我認爲addError函數是在代碼中的其他地方定義的,並且與問題無關。 –