3

I am trying to incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.

The following is my script:

    var regOrderNo = new RegExp("\d{6}");
    var order_no = $("input[name='txtordernumber']").val();
    alert(regOrderNo.test(order_no));

Why would this not come back with true if the txtordernumber text box value was a six digit number or more?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Billy Logan
  • 2,833
  • 16
  • 43
  • 49

3 Answers3

7

You have to escape your \ when used inside a string.

new RegExp("\\d{6}");

or

/\d{6}/
Chetan S
  • 23,637
  • 2
  • 63
  • 78
4

Insert an extra "\" in your regexp.

Cleiton
  • 17,663
  • 13
  • 46
  • 59
3

You need to escape your backslash. It's looking for "\d", not digits.

So...

var regOrderNo = new RegExp("\\d{6}");
Illandril
  • 656
  • 4
  • 11