Javascript regex to match a six digit number

I am trying to include a regex that I used differently in the past in some validation check via JavaScript.

Below is my script:

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

Why not return with true if the text box txtordernumberwas a six-digit number or more?

+3
source share
3 answers

Insert an extra "\" in your regular expression.

+3
source

When used inside a string, you need to avoid \.

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

or

/\d{6}/
+5
source

. "\ d", .

...

var regOrderNo = new RegExp("\\d{6}");
+3
source

Source: https://habr.com/ru/post/1717877/


All Articles