Custom validation | XM Community
Skip to main content
Question

Custom validation

  • 6 June 2024
  • 2 replies
  • 21 views

Hello,

I’d like partipants to answer “ first 2 letter of first name” & ‘first 3 letter of last name’ in the from field. How can I validate so that they can only enter 2 letters and 3 letters on that field?

 

(new to the field) Thank you!

 

2 replies

Userlevel 3
Badge +13

Hey @Tafroj 

for the first question with 2 letters of the first name use this code:
Qualtrics.SurveyEngine.addOnload(function() {

    var questionId = this.questionId;

    var input = document.querySelector('#'+questionId+' .InputText');

    input.setAttribute('maxlength', '2'); 

    

    input.addEventListener('input', function() {

        this.value = this.value.replace(/[^a-zA-Z]/g, '').toUpperCase().slice(0, 2);

    });

});


for the question with the 3 letters of the last name use this code:
Qualtrics.SurveyEngine.addOnload(function() {

    var questionId = this.questionId;

    var input = document.querySelector('#'+questionId+' .InputText');

    input.setAttribute('maxlength', '3'); 

    

    input.addEventListener('input', function() {

        this.value = this.value.replace(/[^a-zA-Z]/g, '').toUpperCase().slice(0, 3);

    });

});


For the question with the 2 digits of the month us this code:
Qualtrics.SurveyEngine.addOnload(function() {

    var questionId = this.questionId;

    var input = document.querySelector('#'+questionId+' .InputText');

    input.setAttribute('maxlength', '2'); 

    

    input.addEventListener('input', function() {

        this.value = this.value.replace(/[^0-9]/g, '').slice(0, 2); 

    });

});

 

Badge +9

Hi @Tafroj ,

 

Perhaps you can use match regex.

First field condition: ^[A-Za-z]{2}$

and

Second field condition: ^[A-Za-z]{3}$

and

Third field condition: ^\b(0[1-9]|1[0-2])\b$

 

Leave a Reply