help building a regular expression to validate a date field

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   help building a regular expression to validate a date field
mearnheart
CZ Newbie
mearnheart has been a member for over 19 year's 19 Year Member
Gender: Male
Status: Offline
Joined: Jul 29, 2005
0.00 posts per day
Posts: 6
Points: 261
   
I need help creating a regular expression to validate a date field in a form.
The actuall call to validate the form is working fine, I just can not seem to get the regular expression correct. I know JACK about RegExp's.

valid date form: YYYY-MM-DD
example: 2004-09-29

Here is what I have


  if(document.form1.issue_date.value=="")
  {
    alert("Please indicate valid Issue Date!")
    document.form1.issue_date.focus()
    return false
  }
  else
  {
    var reg = /\d(4)-\d(2)-\d(2)/
   
    if(reg.test(document.form1.issue_date.value))
   {
      alert("This IS a valid date!")
      return true
    }
    else
    {
      alert("This IS NOT a valid date!")
      document.form1.issue_date.focus()
      return false
    }
  }


TIA,
Mike


Back to top Reply with quote
#2   re: help building a regular expression to validate a date fi
GoddsEgo
PayPal Donation
CZ Moderator
Status: Offline
Joined: Jun 26, 2003
0.16 posts per day
Posts: 1211
Points: 69,166
 Yahoo Messenger  
Hi Mearnheart

How about ...

var objRegExp = /^\d{4}(\-)\d{1,2}\1\d{1,2}/


Breaking it down...

/ : the opening and closing tag to string

^\d{4} : that the string begin with four digits

(\-) : that following the four digits there is an "-"

\d{1,2} : the next portion of string will be one to two digits

\1 : that there is another occurance of the string value to the left that is in parenthesized

\d{1,2} : the next portion of string will be one to two digits

/ : the opening and closing tag to string




As for the script its self how about...

var IssuDate = document.form1.issue_date
var objRegExp = /^\d{4}(\-)\d{1,2}\1\d{1,2}/

if(!objRegExp.test(IssuDate.value)) {
    alert("Please indicate valid Issue Date!")
    IssuDate.focus()
    return false
     }else{
   alert("This IS a valid date!")
   return true
    }





NOTE: The pattern and script you have here will ONLY states what the string consist of character and format wise and does not validate whether the date its self is valid


Back to top Reply with quote
#3   (resolved) help building a regular expression ....
mearnheart
CZ Newbie
mearnheart has been a member for over 19 year's 19 Year Member
Gender: Male
Status: Offline
Joined: Jul 29, 2005
0.00 posts per day
Posts: 6
Points: 261
   
thank you very much, worked wonderfully....

mike



Back to top Reply with quote
#4   
GoddsEgo
PayPal Donation
CZ Moderator
Status: Offline
Joined: Jun 26, 2003
0.16 posts per day
Posts: 1211
Points: 69,166
 Yahoo Messenger  
yw



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum