5 Replies - 1411 Views - Last Post: 02 May 2010 - 08:17 AM

#1 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 374
  • Joined: 11-July 07

A little RegEx

Posted 25 April 2010 - 08:31 AM

OK, I'm validating my registration form with Javascript/jQuery.

I am now having trouble validating a format of a Date of Birth.

Here is my RegEx:
var regex_dob = /([0-9]{2})\/([0-9]{2})\/([0-9]{4})/



But it seems to be saying anything is valid?

Please help, thanks.



-huzi

This post has been edited by huzi8t9: 25 April 2010 - 08:42 AM

Is This A Good Question/Topic? 0
  • +

Replies To: A little RegEx

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: A little RegEx

Posted 25 April 2010 - 03:22 PM

The problem you have is that you are saying the digits are optional.

Try this:
var re = /^(\d{2}\/){2}\d{4}$/;
alert(re.test(dob));


It works fine in IE8, and should be cross-browser. If you need an explanation I can provide one.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 374
  • Joined: 11-July 07

Re: A little RegEx

Posted 26 April 2010 - 11:38 AM

View PostBetaWar, on 25 April 2010 - 09:22 PM, said:

The problem you have is that you are saying the digits are optional.

Try this:
var re = /^(\d{2}\/){2}\d{4}$/;
alert(re.test(dob));


It works fine in IE8, and should be cross-browser. If you need an explanation I can provide one.

Hope that helps.


Thanks alot.

I'm sort of getting used to RegEx, after all this time; would I be right in assuming that the "^" and "$" made all the difference?

would
var regex = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;


be the same as
var regex = /^(\d{2}\/){2}\d{4}$/;



Please guide me where I am mistaken.

Thank you again



-huzi
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: A little RegEx

Posted 26 April 2010 - 11:47 AM

Yes, it seems to work the same for both of them. Using the \d is a special character combination for regex that will match any digit character.
Was This Post Helpful? 1
  • +
  • -

#5 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: A little RegEx

Posted 01 May 2010 - 06:26 AM

View Posthuzi8t9, on 26 April 2010 - 08:38 PM, said:

I'm sort of getting used to RegEx, after all this time; would I be right in assuming that the "^" and "$" made all the difference?


The "^" matches the beginning of the tested string saying that there can't be anything else before the match.
The "$" matches the end of the tested string saying that there can't be anything else after the match.

By using both, as provided by BetaWar, you state that the whole string must match the regular expression or else it is invalid! :)
Was This Post Helpful? 1
  • +
  • -

#6 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 374
  • Joined: 11-July 07

Re: A little RegEx

Posted 02 May 2010 - 08:17 AM

View PostWimpy, on 01 May 2010 - 12:26 PM, said:

View Posthuzi8t9, on 26 April 2010 - 08:38 PM, said:

I'm sort of getting used to RegEx, after all this time; would I be right in assuming that the "^" and "$" made all the difference?


The "^" matches the beginning of the tested string saying that there can't be anything else before the match.
The "$" matches the end of the tested string saying that there can't be anything else after the match.

By using both, as provided by BetaWar, you state that the whole string must match the regular expression or else it is invalid! :)


Ahh!! that helps a lot! Now I understand the characters, thank-you very much :bigsmile:


-huzi
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1