Using Regex for a custom validation | XM Community
Skip to main content
Solved

Using Regex for a custom validation


Hi,
I've an ID that follows a specific format, & I want to enforce that this format is adhered to during input using custom validation. Looks like I might be able to do this using Regex. I'm new to Regex, & haven't been able to get my head around it just yet:

The ID is made of 3 parts following this format:
Always starts with P0
then 2 numbers
then 3 numbers

Any help/advice would be greatly appreciated!
Thanks!

Best answer by ahmedA

Matches Regex:

^P0\\d{5}$

View original

15 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • Answer
  • February 19, 2021

Matches Regex:

^P0\\d{5}$


  • Author
  • 1 reply
  • February 19, 2021

Perfect, thank you! (I see where I as going wrong now).


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • February 19, 2021

I good place to get started with Regex is www.regex101.com. It provides quite a good explanation for the expression one enters.


Forum|alt.badge.img
  • 4 replies
  • September 6, 2022

Hi,
Similarly, I also need an ID that follows a specific format for custom validation. I am totally clueless about REGEX, can someone please help?
The ID is made of 3 parts following this format:
Always starts with a "B"
then 1-digit or 2-digit number (maximum 2)
followed by a "-"
then 1-digit or 2-digit number (maximum 2)

for example, B8-2, B12-8, or B11-13
Any help/advice would be greatly appreciated!
Thank you!


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5935 replies
  • September 6, 2022

Forum|alt.badge.img
  • 4 replies
  • September 6, 2022

Thank you, Tom! You are my lifesaver! It worked!


Forum|alt.badge.img
  • 4 replies
  • September 8, 2022

Hi,
I now find out that some of the IDs may include alphabets (small letters or CAP). So, the custom validation needs to follow the following format:
Always starts with a "B"
then 1-digit or 2-digit number or alphabet (small or CAP) [maximum 2]
followed by a "-"
then 1-digit or 2-digit number or alphabet (small or CAP) [maximum 2]
for example, B8-2a, B12-c8, or B11-VA
How should the Regex be changed to? Please help. Thank you in advance!


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5935 replies
  • September 8, 2022

Forum|alt.badge.img
  • 4 replies
  • September 9, 2022

Thank you, Tom! I'm so grateful for your help!


Forum|alt.badge.img
  • 3 replies
  • October 9, 2023

Hi,

I’m new to regex. If I wanted the ID to be text only with a maximum of 6 letters long. What would be the expression for this?

 

Thank you,

Keri


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • 1091 replies
  • October 9, 2023
kerikw wrote:

Hi,

I’m new to regex. If I wanted the ID to be text only with a maximum of 6 letters long. What would be the expression for this?

 

Thank you,

Keri

@kerikw  ^[A-Za-z]{1,6}$


Forum|alt.badge.img
  • 3 replies
  • October 9, 2023

Thanks a lot Nam Nguyen! 

 

If I have a preset list, do you know whether I can match the answer in the form to that list? I realised that I don’t have access to ‘autocomplete’ and I’m trying to match participant’s ID number of their school to an existing list that I have. I’m not sure what the best way of doing this is. Any help would be most appreciated! 

E.g.,

I have pre-assigned a list of codes to specific schools (e.g. ,SchoolID).

I’d like to have participants type in this ‘schoolID’ in the form. And I want to double-check that what they have typed in matched the ‘SchoolID’ from my existing database/list. 

How would I set this up?

 

Best,

Keri


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5935 replies
  • October 9, 2023

You could convert the school id form field to a drop-down.  If you are interested, the formSelects function does that.


Forum|alt.badge.img
  • 3 replies
  • October 9, 2023

Thanks Tom! 

I thought of that as an option too, but the list will be in the 150+ and because of GDPR, I’m hoping that participants don’t need to see which other schools are taking part. 

 

Do you think there’s another way of matching a participant’s response on the form to a database?

 

Best,
Keri


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5935 replies
  • October 9, 2023

@kerikw,

Yes, you could put all the codes in an array, and check to make sure the entered code is in the array. I would add a hidden field to flag correct answers and base the validation off that.  


Leave a Reply