2013-08-22 27 views
1

我有一個標有「得分」文本框。我希望用戶輸入介於0和100之間的值。我如何驗證用戶提供的值?驗證一個文本框接受0到100之間的數字,多數民衆贊成只

HTML:

<div class="editor-label"> Score </div> 
<div class="editor-field"> 
    <input type="text" name="Score" id="Score" maxlength="25" /> 
    <span id="ScoreError" class="error"></span> 
</div> 

我試過到目前爲止:

var num = document.getElementById("Score").value; 
if (num < 0 || num > 100){ 
    document.getElementById("ScoreError").innerHTML = "- Score is required."; 
    alert("Enter a Score thats between 0 to 100") 
    isValidForm = false; 
} 
+1

你爲什麼不使用'',_type_設置爲'number',_min_'0',_max_'100'和_value_'0'? http://jsfiddle.net/A7EFa/ –

+0

看看[這裏](http://utilitymill.com/utility/Regex_For_Range) – BrOSs

+0

請[編輯]你的問題,而不是在評論張貼代碼。這個條件對我來說確實很好。何時/如何運行代碼? –

回答

0

你可以擺脫使用var num = document.getElementById("Score").value.replace(/[^0-9]/g, "")然後任何非數字字符您的if/then語句將尋找一個整數。

這裏有一個演示:http://jsfiddle.net/7FXxt/

相關問題