-1

I am looking for a Javascript regular expression which has 2-4 letters, followed by a dash, and then 5-6 numbers.

Valid examples would be: AB-123456, ABC-12345, ABCD-123456

I tried checking data entered in a field and then try the regex like this:

$('#inputid').change(function() {
        var myregex = '/^([a-zA-Z]){2,4}-([0-9]){5,6}$/';
        if(myregex.test($(this).val()) == false)
        {
            alert("Bad data");
        }
    });
William Ross
  • 3,568
  • 7
  • 42
  • 73

1 Answers1

4

define regular expression without string quotation, like this,

var myregex = /^([a-zA-Z]){2,4}-([0-9]){5,6}$/;

then the myregex.test('AB-123456') will work