Ensuring answer includes letter and number

I have a patient questionnaire, at the top I have ‘patient code’ so we can anonymise and link patients using different forms. The code consists of letter ‘C’ or ‘P’ (patients or controls) and then a number e.g. C11, P15 etc. I was wondering if there was a way I can make the answer only valid if it includes either a P or a C, plus a number (ANY number) to help avoid errors.

Thank you!

Try a regex constraint; eg

constraint = regex(., '^[CP][0-9]+$')

[note: you didnt specify if ‘0’ is considered a valid number, nor the min/max # of digits. Both would affect the regex expression needed]


Hi there
Thank you so much! I don’t think I need a min or max - if I did what would i input? e.g. if min was 1 and max was 5?

Thank you!

You will need to add {min, max} to your regex e.g.

constraint = regex(., '^[CP][0-9]{1,5}+&)`


1 Like

if by this you mean you want at least 1 digit and at most 5 digits, then @stephanealoo change above should suffice. However, if you mean a number between 1 and 5, then you’d have to check for just those digits, ie

regex(., '^[CP][1-5]$)`

Basically, regex’s check a string’s character contents, and do not perform actual numeric comparisons per se. So if, for example, you wanted a number between 42 and 871, that actually involves a pretty messy regex to check for valid combinations of digits (and I’d probably suggest a different approach…)