2014-02-13 35 views
0

你能幫我解決我的代碼嗎? 我有一個文本框,將接受字母數字,但我想包括空格鍵和週期..javascript中的文本框驗證

請幫助我..謝謝

<html> 
<head> 
<script type='text/javascript'> 
var specialKeys = new Array(); 
      specialKeys.push(8); //Backspace 
      specialKeys.push(9); //Tab 
      specialKeys.push(46); //Delete 
      specialKeys.push(36); //Home 
      specialKeys.push(35); //End 
      specialKeys.push(37); //Left 
      specialKeys.push(39); //Right 
      specialKeys.push(38); //Up 
      specialKeys.push(40); //Down 
      specialKeys.push(190); //Period 
      specialKeys.push(32); //Space 
     function IsAlphaNumeric(e) 
      { 
       var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode; 
       var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 96 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode)); 
       document.getElementById("error1").style.display = ret ? "none" : "inline"; 

       return ret; 
      } 
</script> 
</head> 
<body> 

<div id="error1" style="width:300px;background:#FF9999;border:1px solid #FF1919;height:18px;text-align:center;font-family:Verdana;font-size:10px;line-height:18px;display:none;">&nbsp;&nbsp;ERROR : Special Characters not allowed&nbsp;&nbsp;</div><br> 
               <input type='text' name='customerName' style='width:400px;text-transform:uppercase' onkeypress="return IsAlphaNumeric(event);"/> 

</body> 
</html> 
+0

我認爲你應該嘗試RegEx。 –

回答

0

使用正則表達式來驗證輸入,這樣的:http://jsfiddle.net/maximgladkov/exyVL/

function IsAlphaNumeric(e) { 
    var result = e.keyCode > 0 || /^[0-9a-zA-Z .?\/]$/.test(String.fromCharCode(e.charCode)); 
    document.getElementById("error1").style.display = result ? "none" : "inline"; 

    return result; 
}; 
+0

非常感謝你最大.. 我有另一個問題..如何啓用退格在那裏? – lei

+0

也是特殊字符/? 非常感謝你..我真的很感謝你的回答 – lei

+0

這很奇怪,退格在爲我工作。 –

1

你可以只添加(的keyCode == 32 ||鍵代碼== 46),以支票爲接受的字符:

<html> 
<head> 
<script type='text/javascript'> 
var specialKeys = new Array(); 
      specialKeys.push(8); //Backspace 
      specialKeys.push(9); //Tab 
      specialKeys.push(46); //Delete 
      specialKeys.push(36); //Home 
      specialKeys.push(35); //End 
      specialKeys.push(37); //Left 
      specialKeys.push(39); //Right 
      specialKeys.push(38); //Up 
      specialKeys.push(40); //Down 

     function IsAlphaNumeric(e) 
      { 
       var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode; 
       var ret = ((keyCode == 32 || keyCode == 46) || (keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 96 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode)); 
       document.getElementById("error1").style.display = ret ? "none" : "inline"; 

       return ret; 
      } 
</script> 
</head> 
<body> 

<div id="error1" style="width:300px;background:#FF9999;border:1px solid #FF1919;height:18px;text-align:center;font-family:Verdana;font-size:10px;line-height:18px;display:none;">&nbsp;&nbsp;ERROR : Special Characters not allowed&nbsp;&nbsp;</div><br> 
               <input type='text' name='customerName' style='width:400px;text-transform:uppercase' onkeypress="return IsAlphaNumeric(event);"/> 

</body> 
</html> 
+0

它不起作用:( 它將允許所有的特殊字符.. – lei

+0

我只想接受字母數字空格和句點(。)只.. – lei

+0

對不起!週期是鍵碼46,只是嘗試我編輯的代碼 –