admin管理员组文章数量:1415475
I wonder what seems to be the problem in here.
$("#city_field").bind({
keypress: function(event){
var regex = /^[a-zA-ZäöüÄÖÜ]/;
if (regex.test($("#city_field").val())==true) {
$('.r_validation').html('').css('background-color', '');
} else {
$('.validation').html("TEST").css('background-color', 'red');
}
}
});
Whenever I input letters it is valid, then when I input numbers the red validation notice appears. Everything is fine at this point, not until I entered numbers after letters. The following should be invalid, but in my current script it is seen as valid:
- foo1212
- foo,
- foo, [with space after ma]
What I really want to validate is to ONLY accept letters and dash (-), nothing more other than that.
Thanks.
EDIT
For the record, the last ment of @Adam Rackis solved the issue, I ended up with this working code.
$(function(){
$("#city_field").bind("keyup",
function(event) {
var regex = /^[a-zA-ZäöüÄÖÜ]+$/;
if (regex.test($("#city_field").val())==true) {
$('.validation').html('').css('background-color', '');
} else {
$('.validation').html("TEST").css('background-color', 'red');
}
});
});
I wonder what seems to be the problem in here.
$("#city_field").bind({
keypress: function(event){
var regex = /^[a-zA-ZäöüÄÖÜ]/;
if (regex.test($("#city_field").val())==true) {
$('.r_validation').html('').css('background-color', '');
} else {
$('.validation').html("TEST").css('background-color', 'red');
}
}
});
Whenever I input letters it is valid, then when I input numbers the red validation notice appears. Everything is fine at this point, not until I entered numbers after letters. The following should be invalid, but in my current script it is seen as valid:
- foo1212
- foo,
- foo, [with space after ma]
What I really want to validate is to ONLY accept letters and dash (-), nothing more other than that.
Thanks.
EDIT
For the record, the last ment of @Adam Rackis solved the issue, I ended up with this working code.
$(function(){
$("#city_field").bind("keyup",
function(event) {
var regex = /^[a-zA-ZäöüÄÖÜ]+$/;
if (regex.test($("#city_field").val())==true) {
$('.validation').html('').css('background-color', '');
} else {
$('.validation').html("TEST").css('background-color', 'red');
}
});
});
Share
Improve this question
edited Jan 13, 2012 at 6:12
planet x
asked Jan 13, 2012 at 5:36
planet xplanet x
1,6275 gold badges27 silver badges44 bronze badges
1 Answer
Reset to default 4Your regex is matching only one character. You need to put that input over a Kleene closure, then put an end of string marker:
/^[a-zA-ZäöüÄÖÜ]*$/;
Or if you want to make sure there's at least one character, you'd use the positive closure:
/^[a-zA-ZäöüÄÖÜ]+$/;
Which produces:
var r = /^[a-zA-ZäöüÄÖÜ]+$/;
console.log(r.test('Foo')); //true
console.log(r.test('Foo123')); //false
console.log(r.test('foo,')); //false
console.log(r.test('foo, ')); //false
console.log(r.test('foo ')); //false
DEMO
ALSO, make sure you catch the keyup
event, since keypress
fires before the text in your textbox updates, so you're always validating one character behind.
So on your Regex:
/^[a-zA-ZäöüÄÖÜ]/;
When testing Foo123
the F
would be consumed, and that's it. Done.
本文标签: javascriptregex letters only keypress as eventStack Overflow
版权声明:本文标题:javascript - regex letters only keypress as event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745149033a2644832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论