Regex Pattern which Only allow 4 digit

Radhe
Tera Expert

Hi All.

@Mark Roethof 

I have one variable  (Type - single line text) and I need Regex Pattern which allow only 4 digit (number) in this field

 

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

To match exactly 4 digits:

/^\d{4}$/
^\d{4}$

 

Not more then 4:

/^\d{0,4}$/
^\d{0,4}$

View solution in original post

7 REPLIES 7

Willem
Giga Sage
Giga Sage

To match exactly 4 digits:

/^\d{4}$/
^\d{4}$

 

Not more then 4:

/^\d{0,4}$/
^\d{0,4}$

Mark Roethof
Tera Patron
Tera Patron

Hi there,

For exactly 4 digits, this would work:

\d{4}$

or for up to 4:

\d{0,4}$ 

Since it's a variable, please don't go for client script. Use the variable validation regex option.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

For example:

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Sushma Dhandge
Giga Contributor

Hi Radhe,

In regex pattern you can use both way to allow only 4 digit in the field.

To match four digit exactly

 /^[0-9]{4}$/

OR

/^\d{4}$/

please mark helpful if you find it correct.

Thanks and Regards

Sushma Dhandge