Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Adding Validations to Token Fields

  • LSCitizen
  • LSCitizen's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 years 4 months ago #89253 by LSCitizen
Adding Validations to Token Fields was created by LSCitizen
Can validation (like regular expression) be added to token fields?

For example, I want to create a token field called Date of Birth, but I want the format of the date to be in MM-DD-YYYY format.
Can I add a regular expression like /^\d{2}-\d{2}-\d{4}$/?
The topic has been locked.
  • c_schmitz
  • c_schmitz's Avatar
  • Offline
  • LimeSurvey GmbH Employee
  • LimeSurvey GmbH Employee
More
11 years 4 months ago #89254 by c_schmitz
Replied by c_schmitz on topic Adding Validations to Token Fields
No sorry, this is currently not possible.

Best regards

Carsten Schmitz
LimeSurvey project leader
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 3 months ago - 11 years 3 months ago #89317 by tpartner
Replied by tpartner on topic Adding Validations to Token Fields
As c_schmitz says, you can't do this server-side but there are several client-side solutions. (It should be noted that these would only work if JavaScript is enabled in the respondent's browser and they don't actively hack the JS)

1) Validate the input before submitting the form. Add something like the following to the end of template.js. Replace "attribute_1" with your attribute field. (This snippet was developed for the default template and may need to be modified for others):
Code:
$(document).ready(function(){
 
  $('.register-form form').submit(function(){
 
    var attributeField = 'attribute_1';
    var errorMessage = 'Please use date format mm-dd-yyyy'; 
 
    var validFormat = /^(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d$/; 
 
    // Test date format
    if(!validFormat.test($('input[name="register_'+attributeField+'"]').val())) {
      alert(errorMessage);
      return false;
    }
  });
 
});

2) Use a jQuery date picker :
Code:
$(document).ready(function(){
 
  var attributeField = 'attribute_1';
 
  $('input[name="register_'+attributeField+'"]').datepicker({ 
    dateFormat: 'mm-dd-yy'
  }).attr('readonly', true);
 
});

3) Use an input mask - digitalbush.com/projects/masked-input-plugin/

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 11 years 3 months ago by tpartner.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose