SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help with this reg expression

    Hi All

    I am trying to put together a form validation function. I want to make sure that the user is entering the date in the correct format.

    the format is as follows YYYY-MM-DD (e.g. 2002-06-26).

    I want to valid the field using both JavaScript and PHP. Could someone tell me what the regular expressions for both these languages are.

    I have been trying the following without success:

    JavaScript:
    var regexp = /^\d{4}\-\d{2}\-\d{2}$/;

    PHP:
    Haven't got that far yet. Any suggestions?

    Kind regards, Ben
    Last edited by ben_m_gunn; Jun 27, 2002 at 22:33.

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PLEASE DISREGARD THE ABOVE POST. I just found my answer in another tutorial. Problem wasn't the expression, it was the implementation. For anyone else who might want to know, you valid the expression like so:

    var objRegExp = /^\d{4}\-\d{2}\-\d{2}$/;
    if (!objRegExp.test(date))
    {
    alert('Not a valid date!\nDate should follow YYYY-MM-DD format');
    }

    Ben


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •