Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Postal Code Validation

I want to include postal code validation on the postal codes I am collecting in my study.

My postal codes are Canadian.

They are being collected in a Respondent Specify box, sometimes in a select/check box/respondent specify question/response, sometimes in a grid/respondent specify question/response.

I have the regex for it, but I do not know how to implement it in my study.


Thank you for any help.
asked Jun 18, 2018 by Amber Joy (175 points)

1 Answer

+1 vote
In your question's settings, look for an "Advanced" button.  Within there, you can find and enable custom JavaScript verification.  Give this verification a try:

var other = SSI_GetValue('SelectQ_1_other');
if (other && !other.match(regex)) {
    strErrorMessage = 'Error message here.';

"SelectQ_1_other" should be replaced with the variable name of the other specify field you want to validate.

I took the liberty of making your regular expression case-insensitive.  If you wish to remove this, simply remove the "i" near the end of line 1.
answered Jun 18, 2018 by Zachary Platinum Sawtooth Software, Inc. (114,625 points)
This is most excellent.  Thank you.  I had it almost correct, but was placing the code in the wrong section of the question.  Thank you, Zachary!
I have a selection question:

I1.  Can you please give use your email address?

1.  Email:  (respondent specify)
2.  No

I would like to run postal code validation on I1_1_other if it is used, but not run the validation if they choose response 2.  Can you please show me how to run the validation only if I1_1_other is answered?
If the other specify is set to require a response, that behavior should already be accounted for.
If I choose option 2, which is that I do not want to give an email address, I am given the error message that is indicated in the javascript above and am unable to proceed.

In the settings of the Other Respondent Specify, I have it set to Not Required with min characters 0 and max characters 100.
I am not observing anything of the sort.  What version of the software are you running?
Lighthouse Studio 9.5.3
I fear I wasn't very clear in explaining my issue and the context.

I am using Lighthouse Studio 9.5.3, and I am using what you taught me above to validate email addresses as follows:

var regex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
var resp = SSI_GetValue('I1_1_other');
if (!regex.test(resp)) {
    strErrorMessage = 'Error - please enter a valid email address';

I have this set up in a select question named I1.

The question asks respondent to give an email address.


1.  Email address:  (Respondent Specify - in settings there is no checkmark for Required)
2.  No

If respondent clicks on response 2 to indicate they do not want to give an email address, they are shown the error message in the above javascript and are unable to proceed.
In my code, the conditional uses the truthiness of the other specify response (i.e., whether the field was selected) as well as the regular expression test.  You have removed the first part in your code, causing this behavior.

If you need this part of the script to stay removed, then you'll need to use SSI_GetValue in the conditional to only fire the error message when the first response option is selected:

SSI_GetValue('I1') == 1
I did not intentionally change/remove that code, and didn't realize it had happened!  I feel a bit silly now, but I really appreciate your patience and assistance.  The code all works exactly as it should, and I will include it below for anyone else who may search this question later:

var regex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
var other = SSI_GetValue('I1_1_other');
if (other && !other.match(regex)) {
    strErrorMessage = 'Error - please enter a valid email address';