入力文字と空白のみを許可するには、jquery または js 関数が必要です。前もって感謝します。
ページ:
<p:inputText onkeypress="onlyLetter(this)">
関数:
function onlyLetter(input){
$(input).keypress(function(ev) {
var keyCode = window.event ? ev.keyCode : ev.which;
// code
});
}
入力文字と空白のみを許可するには、jquery または js 関数が必要です。前もって感謝します。
ページ:
<p:inputText onkeypress="onlyLetter(this)">
関数:
function onlyLetter(input){
$(input).keypress(function(ev) {
var keyCode = window.event ? ev.keyCode : ev.which;
// code
});
}
次のコードでは、az、AZ、および空白のみを使用できます。
HTML
<input id="inputTextBox" type="text" />
jQuery
$(document).on('keypress', '#inputTextBox', function (event) {
var regex = new RegExp("^[a-zA-Z ]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
無効にしたい、または機能しないようにしたいキー/数字のASCIIコード(10進数値)を使用するだけです。アスキー テーブル。
HTML :
<input id="inputTextBox" type="text" />
jQuery :
$(document).ready(function(){
$("#inputTextBox").keydown(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
});
まず、私は jQuery の経験がほとんどないので、バニラの JavaScript の例を提供します。ここにあります:
document.getElementById('inputid').onkeypress=function(e){
if(!(/[a-z ]/i.test(String.fromCharCode(e.keyCode))) {
e.preventDefault();
return false;
}
}
Ashad Shanto の回答を微調整します。スクリプトを使用する場合、y と z を入力できないことに注意してください。inputValue を 120 から 123 に変更する必要があります。ASCII テーブル リファレンスは次のとおりです。すべての文字、スペース、およびバックスペースで。
<script>
$(document).ready(function(){
$("#inputTextBox").keypress(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
console.log(inputValue);
});
});
</script>
function ValidateAlpha(evt) {
var keyCode = (evt.which) ? evt.which : evt.keyCode if (
(keyCode < 65 || keyCode > 90) &&
(keyCode < 97 || keyCode > 123) &&
keyCode != 32 &&
keyCode != 39
)