Parvathy
Parvathy

Reputation: 2445

How can I format a number like this xxxx-xx-xxxx

I'm using antd form item and I tried the below pattern but it's not working.

 rules: [{
     pattern: new RegExp("^d{4}-d{2}-d{4}$"),
     message: "Please check  format"
 }]

Upvotes: 2

Views: 506

Answers (2)

voukvouk
voukvouk

Reputation: 121

Try using your RegEx like this:

^\d{3}-\d{3}-\d{4}$

Upvotes: 0

jo_va
jo_va

Reputation: 13964

You have to specify the digits as \d and not just d. Also, you can use a literal expression like this:

pattern: /^\d{4}-\d{2}-\d{4}$/

Or define it as a string passed to the RegExp constructor but you will have to escape the backslashes, leading to \\d instead of \d:

pattern: new RegExp('^\\d{4}-\\d{2}-\\d{4}$')

You can use a tool like Regex101 to help build and validate your regular expressions.

Upvotes: 1

Related Questions