Regex validation questions with strings and numbers | XM Community
Skip to main content
Solved

Regex validation questions with strings and numbers


Hello, Our group is developing surveys for a large study, and we give each participant an ID number. For new participants, the format is "AJA-XXXX" where the X's are digits 0-9. For visiting participants, the format is "AJA-XXXX.X." Here are some ID numbers that fit: > * 1. AJA-2001 > * 2. AJA-2163 > * 3. AJA-2003.1 > * 4. AJA-2002.2 > However, we don't want the following: > * aja-2002 (first part not capitalized) > * AJA3012 (no dash) > * AJA-2009. (a period, but no digit afterwards) > * AJA-1008.01 (two digits after the period) > After some googling, I came up with ``[T][R][T]\\-\\d{4}[\\.]?[\\d{1}]?`` but it didn't work :/ I would be extremely grateful for any help or suggestions. Thanks!

Best answer by TomG

Try this regex: ^AJA\\-[0-9]{4}(\\.[0-9])?$ One issue is that Qualtrics regex validation is case insensitive. So, you'll have to use JavaScript to convert to upper case. ``` Qualtrics.SurveyEngine.addOnload(function() { jQuery("#"+this.questionId+" .InputText").on('blur', function() { this.value = this.value.toUpperCase(); }); }); ```
View original

4 replies

Forum|alt.badge.img+1
  • QPN Level 2 ●●
  • 25 replies
  • August 17, 2018
I have tried this through javascript.. jQuery("[id='QR~QID15']").bind('keyup', function(e) { var s=jQuery("[id='QR~QID15']").val(); if(s.length == 8) { if(s.substring(0,3) != "AJA" || s.charAt(3) != '-' ) { alert("Wrong input"); } else { var n=parseInt( s.substring(4,8) ); var i=0; while(n!=0) { n=Math.floor(n/10); i++; } if(i != s.substring(4,8).length) alert("Wrong input"); } } if(s.length>8) alert("Wrong input"); });

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5932 replies
  • Answer
  • August 17, 2018
Try this regex: ^AJA\\-[0-9]{4}(\\.[0-9])?$ One issue is that Qualtrics regex validation is case insensitive. So, you'll have to use JavaScript to convert to upper case. ``` Qualtrics.SurveyEngine.addOnload(function() { jQuery("#"+this.questionId+" .InputText").on('blur', function() { this.value = this.value.toUpperCase(); }); }); ```

PraDeepKotian_XM
QPN Level 5 ●●●●●
Forum|alt.badge.img+21
  • QPN Level 5 ●●●●●
  • 228 replies
  • August 17, 2018
hi @sajeevan , Regex for this foramt e.g AJA-2001.1: /AJA-[0-9]{4}.[0-9]{1}/g OR Regex for this foramt e.g AJA-2001: /AJA-[0-9]{4}/g You can use this regex in custom validation options. !

Forum|alt.badge.img+1
  • Level 1 ●
  • 8 replies
  • January 22, 2024

To whoever is searching.

 

For the insensitivity of the letters I simply added the “/” before and after then if works : /^[A-Z]{2}$/

I struggle a long time before figuring that out. Without the “/”s, it is indeed insensitive.


Leave a Reply