Closed Thread Icon

Topic awaiting preservation: Using Regular Expressions to recieve date from user (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27073" title="Pages that link to Topic awaiting preservation: Using Regular Expressions to recieve date from user (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Using Regular Expressions to recieve date from user <span class="small">(Page 1 of 1)</span>\

 
Alexer
Bipolar (III) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 11-30-2005 01:21

I'm not entirely sure what is wrong with my Reg Exp. I'm trying to receive dates in "mm/dd/yyyy" format, but it doesn't seem to matter what is submitted.

code:
birthdatepattern = new RegExp("/^[01][0-9]\/[0123][0-9]\/[012][0-9][0-9][0-9]$/");
    var match = birthdatepattern.test(usersubmittedbirthdate);
  	
  	if (match = false) {
  		
  		alert('This match is false.');  		
  		
  	} else {
          
           Result B

           ....

        }



It doesn't matter if I submit 6/26/87, 26/87, or any other combination of numbers and slashes. It all returns "true". Would someone please explain to me what is wrong with this code?

Thanks.

liorean
Bipolar (III) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 11-30-2005 02:05
quote:

code:


You're assigning the value false, not comparing to (which isn't needed at all).

Also, your pattern probably doesn't match what you want it to...

It matches strings like this:
- Match '/'
- Match BOS
- Match '0' or '1'
...
- Match EOS
- Match '/'

Here's a working test code for something doing what you want without going too indepth:

code:
var
    pass=[false,'01/4/1999','11/01/2083','1/31/1900'],
    fail=[false,'0/12/1999','13/12/1999','1/1/1','09/00/1900','9/32/1999','9/23/1893'],
    re=/^(0?[1-9]|1[0-2])\/(0?[1-9]|[12]\d|3[01])\/(19\d\d|20\d\d)$/,
    t;

while(t=pass.pop())
    alert(t+'\n'+re.test(t));
while(t=fail.pop())
    alert(t+'\n'+re.test(t));




However, do you clearly state for users that mm/dd/yyyy is the date format? Because if you asked me for a date I'd assume you wanted 'dd/mm/yyyy' - because that's how they are written here. Or the international standard 'yyyy-mm-dd' which doesn't have this ambiguity.

--
var Liorean = {
abode: "http://web-graphics.com/",
profile: "http://codingforums.com/member.php?u=5798"};

Alexer
Bipolar (III) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 11-30-2005 22:49
quote:
You're assigning the value false, not comparing to (which isn't needed at all).



That ended up being the problem. I appreciate all the trouble you went to explain the details to me, though. Thanks for pointing that out. I would have never caught it.

quote:
However, do you clearly state for users that mm/dd/yyyy is the date format? Because if you asked me for a date I'd assume you wanted 'dd/mm/yyyy' - because that's how they are written here. Or the international standard 'yyyy-mm-dd' which doesn't have this ambiguity.



At this point I'm the only person entering dates, but thanks for the reminder. Hopefully the form will be well notated when the function is put into use.

Thank you for all of your help.

« BackwardsOnwards »

Show Forum Drop Down Menu