+ Reply to Thread
Results 1 to 4 of 4

Thread: date validation

  1. #1
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    514
    Rep Power
    3

    date validation

    Hi,

    I have a textbox called insexpdate

    I want the user to enter valid date format in mm/dd/yyyy format only

    I call the function onblur event

    Code:
    function callinsexpdate()
    {
    var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 
    alert(re);
      if((document.frm1.insexpdate.value == "") && (!document.frm1.insexpdate.value.match(re)))
      { 
    	  alert("Invalid date format"); 
    	
    	  return false;
      }
    
    
    
    }
    
    but the code is not working

    any idea

    todd

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,354
    Blog Entries
    9
    Rep Power
    19

    Try this function:

    Code:
    function checkdateformat(userinput){
    var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
    return dateformat.test(userinput) //returns true or false depending on userinput
    }
    
    The above function returns a boolean (True or False) value depending on if the date entered is in the correct format or not.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Newbie lewy is on a distinguished road lewy's Avatar
    Join Date
    Mar 2008
    Posts
    40
    Rep Power
    3

    @jmh - that will work, but how do you know it's a correct date? It's more complicated than that
    I could just type in 2/31/0000 and it will validate

    In my opinion, giving users the ability to type in a date is a bad idea as they will make mistakes from time to time.

    Rather than doing the validation this way, why don't you use a calendar picker.
    There are plenty out there on the net.
    Make sure the textbox holding the value is readonly

    Then on the serverside, you can validate that it's a correct date (this because you just can't trust users)

    Besides, it's a good programming practice to always validate input on the server.

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,354
    Blog Entries
    9
    Rep Power
    19

    I completely agree. Just answering the question that was asked. But, to take it one step further, as lewy stated, using a date picker which only allows selection of valid dates would be far better
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread

Similar Threads

  1. textbox validation
    By todd2006 in forum JavaScript Programming
    Replies: 6
    Last Post: July 2nd, 2008, 01:30 PM
  2. checkbox validation
    By todd2006 in forum JavaScript Programming
    Replies: 3
    Last Post: June 18th, 2008, 07:22 PM
  3. Programmatically Add Item to Validation Summary
    By jmurrayhead in forum .Net Code Samples
    Replies: 0
    Last Post: March 20th, 2008, 09:41 AM

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

SEO by vBSEO