Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. validating a date

validating a date

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ayman Mashal
    wrote on last edited by
    #1

    hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

    A R P A R 6 Replies Last reply
    0
    • A Ayman Mashal

      hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

      A Offline
      A Offline
      arun rajappan
      wrote on last edited by
      #2

      hi, i think u can use a regularexpression along with the parser....its up to u arun

      1 Reply Last reply
      0
      • A Ayman Mashal

        hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

        R Offline
        R Offline
        rujuc
        wrote on last edited by
        #3

        Use this function for date validation. //Date validator public static bool validateDateTime(object _datetime) { try{ DateTime testdate = DateTime.Parse(_datetime as string); return true; } catch{ return false; } }

        1 Reply Last reply
        0
        • A Ayman Mashal

          hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

          P Offline
          P Offline
          PandemoniumPasha
          wrote on last edited by
          #4

          if you are using VB.NET there is a function - IsDate() which can be used to validate the date. you could also use compare or range validators, depending on your choice and use, to check the data type. but i don't think these will validate if the date string contains alphabets as expressed in your example. but if you are using C#.NET, i think try-catch block (as someone already mentioned) will do the job.:) hope this helps regards :)

          1 Reply Last reply
          0
          • A Ayman Mashal

            hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

            A Offline
            A Offline
            Ayman Mashal
            wrote on last edited by
            #5

            thank u all the try/catch block works fine

            1 Reply Last reply
            0
            • A Ayman Mashal

              hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

              R Offline
              R Offline
              RSArockiam
              wrote on last edited by
              #6

              try this bool validatedate(string datestr) { try { System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US"); DateTime tmpDate= DateTime.ParseExact(date, "dd-MMM-yyyy", culture); return true; } catch(Exception ex) { return false; } }

              Regards R.Arockiapathinathan

              1 Reply Last reply
              0
              • A Ayman Mashal

                hi i am trying to validate a string that contains a date using DateTime.parse function but this function accepts for example the date "31-feb-2007" !! how can i validate the date to be valid not just the format . thanks

                E Offline
                E Offline
                eggsovereasy
                wrote on last edited by
                #7

                Instead of using exception handling as logic as some people have suggested try something like this:

                public bool ValidateDate(string date)
                {
                DateTime temp;
                return DateTime.TryParse(date, out temp);
                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups