Have an idea?

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

Regular Expression - Javascript validation

Hi all

this should be easy.  I'm probably missing something really basic.

Here's the deal:  16 digit number for the respondent to enter.   I have set up 4 text fields allowing 4 characters each (they are text fields because at least 3 of the sets could easily have a leading zero).

Here's my pattern:

var regcheck = /^([0-9]{4})$/;


and here's how I'm calling it:

  if(!regcheck.test(SSI_GetValue("Q10c_number1")))
  {
  strErrorMessage = "Your membership number consists of 16 digits. No letters or spaces. Please enter in the pattern provided.";
  }


It ALMOST works.  Doesn't allow alpha or spaces.  PROBLEM - also doesn't allow leading zeros!

Have tried \d{4} as well.  Can't for the life of me see what I'm doing wrong!

p.s. using version 7.
asked Nov 16, 2012 by Marion Bronze (3,600 points)

3 Answers

+1 vote
is Q10c_number1 a decimal? ssi might be cutting off the 0
answered Nov 16, 2012 by anonymous
edited Nov 16, 2012
using your regex: 0101 = false, "0101" = true, 1010 = true
The field is a free format text box allowing a maximum of four characters. I did it this way to preserve any leading zeros.
how about this?

 if(!regcheck.test("[% Value(Q10c_number1) %]"))
Ah! Interesting!  Why do you reckon this might give a different response than SSI_GetValue?  Very much looking forward to trying it early Monday. (Family have ganged up on me - no work allowed this weekend. They think I'm reading the paper on my tablet at present).
ya sorry realized on the way home that this probably won't work since you'd need to call it on the page after. this is in the javascript verification on the same page as Q10c?

basic idea is just to coerce the value into being a string. ie the scipt returns its value inside of the quotes and acts like a string. im sure you can get something to work along the same lines though. have a nice weekend.
Your idea has potential - maybe I need to first call the string (using SSI_GetValue), then test its length.  If it is less than 4 characters long, concatenate the variable, bedding it out with leading zeros (which the respondent would have had to enter in order to get past the built-in "minimum length" thing).  To be honest, though, I'm surprised we'd need to go through so many loops.
Sorry been busy. Using document.getElementByID("Q10c_number1").value instead of SSI_GetValue seems to do the trick and works with leading zero's.

Tested in v7 and Firefox.
+1 vote
OK here's what the problem was:  SSI_GetValue was being smart and recognising the text field as a numeric because there were only numbers in there.  

Here's how I solved it.

I concatenated the four strings together using concat.  I couldn't use "+" because the blessed thing kept trying to add them together.  

var b1 = (document.mainform["Q10c_number1"].value);
var b2 = (document.mainform["Q10c_number2"].value);
var b3 = (document.mainform["Q10c_number3"].value);
var b4 = (document.mainform["Q10c_number4"].value);

var check = "".concat(b1,b2,b3,b4);

  if(!regcheck.test(check))
  {
  strErrorMessage = "Your membership number consists of 16 digits. No letters or spaces. Please enter in the pattern provided.";
  }



Works a treat, as the first four digits can't begin with zero (hence "check" will never START with zero).  Doesn't quite solve the underlying thing though - how to allow a leading zero - but at least my job can field.
answered Nov 19, 2012 by Marion Bronze (3,600 points)
edited Nov 19, 2012 by Jeff Forkner
0 votes
The problem is your regular expression is wrong.  \d{4} is an incorrect way of saying at least four digits.  You should have \d{4,} if that is what you want.  After you concatenate, your string will be 16 digits long.  It looks like you are trying to verify the whole thing together.  So what you want is

var regcheck = /^\d{16,16}$/;

This will require that only 16 digits, no more, no less, be present.  If anything other character is present, it will fail.
answered Nov 19, 2012 by Mike Lodder Gold (23,395 points)
I changed the test to /^([0-9]{16})$/; and it works a treat.  As I said earlier, I had already tried \d and had no luck.  The problem I had was the leading zero thing.  I was trying to verify each string on its own, and because of the leading zero problem that wasn't happening.  That's why I concatenated and verified it all together as one 16 digit string.  That way, the leading zero issue just went away.  I WILL try the {4,4} thing on my original code though when I get some space clear - would love to solve the leading zero problem for future reference.
...