I have the following code which prevents user from entering space when the length is 0. Now, how can I prevent user from entering all special characters(anything other than a-z A-Z 0-9) when the length is 0?
我有以下代码,当长度为0时,阻止用户输入空格。现在,当长度为0时,如何防止用户输入所有特殊字符(a-z A-Z 0-9以外的任何字符)?
$('#DivisionName').bind('keypress', function(e) {
if($('#DivisionName').val().length == 0){
if (e.which == 32){//space bar
e.preventDefault();
}
}
});
This is my text box.
这是我的文本框。
<input type="text" id="DivisionName" />
3 个解决方案
#1
16
The letter and digit ranges are (inclusive):
字母和数字范围是(包括):
- 97 - 122 (a-z)
- 65 - 90 (A-Z)
- 48 - 57 (0-9)
97 - 122(a-z)
65 - 90(A-Z)
48 - 57(0-9)
This is what you compare e.which
against.
这是你比较e.which反对。
if (e.which < 48 ||
(e.which > 57 && e.which < 65) ||
(e.which > 90 && e.which < 97) ||
e.which > 122) {
e.preventDefault();
}
Or, using inverse logic:
或者,使用逆逻辑:
var valid = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || (e.which >= 97 && e.which <= 122);
if (!valid) {
e.preventDefault();
}
Update
Even so, you may still wish to validate the field contents as a whole using a regular expression:
即便如此,您仍可能希望使用正则表达式整体验证字段内容:
if (/^[A-Z0-9]+$/i.test(value)) {
// it looks okay now
}
Or fix the field by replacing the bad stuff:
或者通过替换坏东西来修复该字段:
var stripped = value.replace(/[^A-Z0-9]+/i, '');
#2
7
This is what you are looking for:
这就是你要找的东西:
$('#DivisionName').bind('keypress', function(e) {
if($('#DivisionName').val().length == 0){
var k = e.which;
var ok = k >= 65 && k <= 90 || // A-Z
k >= 97 && k <= 122 || // a-z
k >= 48 && k <= 57; // 0-9
if (!ok){
e.preventDefault();
}
}
});
or see here: http://jsfiddle.net/D4dcg/
或者看到这里:http://jsfiddle.net/D4dcg/
#3
-1
You can use a regex to validate the string. Something like ^[a-zA-z0-9].*
您可以使用正则表达式来验证字符串。像^ [a-zA-z0-9]这样的东西。*
Here is an article about testing a regex in javascript : http://www.w3schools.com/jsref/jsref_regexp_test.asp
这是一篇关于在javascript中测试正则表达式的文章:http://www.w3schools.com/jsref/jsref_regexp_test.asp
And you can even bind a change event and not a keypress.
您甚至可以绑定更改事件而不是按键。
#1
16
The letter and digit ranges are (inclusive):
字母和数字范围是(包括):
- 97 - 122 (a-z)
- 65 - 90 (A-Z)
- 48 - 57 (0-9)
97 - 122(a-z)
65 - 90(A-Z)
48 - 57(0-9)
This is what you compare e.which
against.
这是你比较e.which反对。
if (e.which < 48 ||
(e.which > 57 && e.which < 65) ||
(e.which > 90 && e.which < 97) ||
e.which > 122) {
e.preventDefault();
}
Or, using inverse logic:
或者,使用逆逻辑:
var valid = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || (e.which >= 97 && e.which <= 122);
if (!valid) {
e.preventDefault();
}
Update
Even so, you may still wish to validate the field contents as a whole using a regular expression:
即便如此,您仍可能希望使用正则表达式整体验证字段内容:
if (/^[A-Z0-9]+$/i.test(value)) {
// it looks okay now
}
Or fix the field by replacing the bad stuff:
或者通过替换坏东西来修复该字段:
var stripped = value.replace(/[^A-Z0-9]+/i, '');
#2
7
This is what you are looking for:
这就是你要找的东西:
$('#DivisionName').bind('keypress', function(e) {
if($('#DivisionName').val().length == 0){
var k = e.which;
var ok = k >= 65 && k <= 90 || // A-Z
k >= 97 && k <= 122 || // a-z
k >= 48 && k <= 57; // 0-9
if (!ok){
e.preventDefault();
}
}
});
or see here: http://jsfiddle.net/D4dcg/
或者看到这里:http://jsfiddle.net/D4dcg/
#3
-1
You can use a regex to validate the string. Something like ^[a-zA-z0-9].*
您可以使用正则表达式来验证字符串。像^ [a-zA-z0-9]这样的东西。*
Here is an article about testing a regex in javascript : http://www.w3schools.com/jsref/jsref_regexp_test.asp
这是一篇关于在javascript中测试正则表达式的文章:http://www.w3schools.com/jsref/jsref_regexp_test.asp
And you can even bind a change event and not a keypress.
您甚至可以绑定更改事件而不是按键。