javascript - regex letters only keypress as event - Stack Overflow

I wonder what seems to be the problem in here.$("#city_field").bind({keypress: function(event

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
Add a ment  | 

1 Answer 1

Reset to default 4

Your 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745149033a4613780.html

相关推荐

  • javascript - regex letters only keypress as event - Stack Overflow

    I wonder what seems to be the problem in here.$("#city_field").bind({keypress: function(event

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信