Kawd
Kawd

Reputation: 4450

Regex that only allows a specific set of characters (Javascript)?

I'm looking for a regex that will only allow characters e, b, z, l to be typed in the user input.

I believe this is wrong ? :

/[ebzl]*/i

I know I need to use a class but I don't know how and couldn't find any relevant tutorials for the above online.

$('input').keyup(function () {
  if (/[ebzl]*/i.test($(this).val())) {
     // do something
  }
});

Upvotes: 0

Views: 44

Answers (1)

Toto
Toto

Reputation: 91415

Anchor your regex:

if (/^[ebzl]+$/i.test($(this).val())) {

Upvotes: 3

Related Questions