Have an idea?

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

How to not require an email response, but still have the Javascript email requirement code?

I'm trying to ask for the respondent's email, but also have the question be optional to answer. I currently have Javascript written in that verifies a valid email was written in, but how do I pass through the code if the input is blank?

Here's what I currently have written in:

function emailvalidate(address, pattern)
{
   if (pattern.test(address) == false)
 {
    return "You entered an invalid email address.";
  } else {
    return "";
  }
}
 
{
  address = SSI_GetValue("Q38_r5_c1");
  pattern = /^([A-Za-z])+([A-Za-z0-9_\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  strErrorMessage = emailvalidate(address, pattern);
}
asked Jan 28 by anonymous

1 Answer

0 votes
Your conditional can be modified to only trigger if the address is non-empty.  While we're at it, I replaced the "== false" with the negation operator "!".

if (address != "" && !pattern.test(address))
answered Jan 28 by Zachary Platinum Sawtooth Software, Inc. (120,175 points)
...