Validating 5-Digit Zip Codes with RegEx

I'm using SSI Web and I'm trying to validate a U.S. zip code so that you must enter at least five digits. I don't want to validate the zip code by looking to see if it is a valid code--I just want to make sure they enter in five numeric digits and not five alpha-numeric characters. Is this possible?

This can be accomplished with a little JavaScript code placed in your question's Advanced Settings > Custom JavaScript Verification tab. The code will use a regular expression pattern checker (also known as RegEx) to determine if the answer has the appropriate number of digits. If it doesn't, it will display an error message that says, "Please revise your five-digit Zip Code" and won't allow the respondent to submit the page until it has been corrected.

First, you will want to compose an open-ended question. Let's name it "zipcode". Then, on the Settings tab > Entry Settings, check the "Require Response" check box, restrict the maximum and minimum number of characters to 5 and set the size of the input box (width) to about 36 pixels.

Next, click the "Advanced..." button to change the Advanced Settings. Click on the Custom JavaScript Verification tab and select the "Call custom JavaScript verification (before or after) the system JavaScript Verification" check box. Then, dump in the following code:

var regex = /^\d{1,5}$/;
var answer = SSI_GetValue("zipcode");
if(regex.test(answer)) { 
else {
  strErrorMessage="Please revise your five-digit Zip Code."; 

Finally, notice that line 2 of the code references "zipcode". That is the name of your question. If you don't name your question "zipcode," make sure you change this bit of code to match what you are using.

Sawtooth Software

6:30 AM to 5:30 PM Mountain Time
(GMT-6; GMT-7 Autumn/Winter)
Monday through Friday

Phone: +1 801 477 4700
Fax: +1 801 337 7410

We are open!

SKIM Software

9:00 AM to 5:30 PM Central European
(GMT+2; GMT+1 Autumn/Winter)
Monday through Friday

Phone: +31 10 282 3500
Fax: +31 10 282 3560

We're open!

Lighthouse Studio

Lighthouse Studio is our flagship software for producing and analyzing online and offline surveys. It contains modules for general interviewing, choice-based conjoint, adaptive choice-based conjoint, adaptive choice analysis, choice-value analysis, and maxdiff exercises.

Try Lighthouse Studio

  Buy Lighthouse Studio