admin管理员组文章数量:1187337
$("#user").keyup(function(e){
var regx = /^[A-Za-z0-9]+$/;
if (!regx.test('#user'))
{$("#infoUser").html("Alphanumeric only allowed !");}
);}
#user
is a text input, and I want to diplay a warning if user enters anything except letters and numbers.
In the above case, the warning is present whatever is typed.
$("#user").keyup(function(e){
var regx = /^[A-Za-z0-9]+$/;
if (!regx.test('#user'))
{$("#infoUser").html("Alphanumeric only allowed !");}
);}
#user
is a text input, and I want to diplay a warning if user enters anything except letters and numbers.
In the above case, the warning is present whatever is typed.
5 Answers
Reset to default 14change:
if (!regx.test('#user'))
to
if (!regx.test( $(this).val() ) )
Do:
$("#user").keyup(function(e){
var str = $.trim( $(this).val() );
if( str != "" ) {
var regx = /^[A-Za-z0-9]+$/;
if (!regx.test(str)) {
$("#infoUser").html("Alphanumeric only allowed !");
}
}
else {
//empty value -- do something here
}
});
JS Fiddle example
You must test with #user element value not '#user' string
$("#user").keyup(function(e){
var regx = /^[A-Za-z0-9]+$/;
if (!regx.test($('#user').val())) // .
{$("#infoUser").html("Alphanumeric only allowed !");}
);}
THis line
regx.test('#user')
has you testing the string #user
, and that is a string that has a bad character (the #
). So it will always say not allowed.
Use the actual value of your $("#user")
there by using $(this).val()
$('#alpha').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
$('#numeric').bind('keypress', function (event) {
var regex = new RegExp("^[0-9\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
$('#alphanumeric').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
$('#alphanumericspecial').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9 .]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
function isNotAlphanumeric(){
return (! val.match(/^[a-zA-Z]+$/))
//return val.match(/^[a-zA-Z0-9]+$/) ? false : true;
}
so if val is alpha numeric it return false. So based on returned value take appropriate action. You can call this method on key up event.
本文标签: jqueryHow to prevent nonalphanumeric input in javascriptStack Overflow
版权声明:本文标题:jquery - How to prevent non-alphanumeric input in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738367338a2081859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
#user
itself, not of the content/value of the element that have the iduser
.!regx.test($(e).val())
should suit your needs. – sp00m Commented Jan 15, 2013 at 10:57