tags:

views:

246

answers:

2

Hi,

I want to validate textbox so that it will accept only future date. Can I have regular expression for the same.

I am using vb.net for coding.

Thanks.

Best Regards, Manoj

+5  A: 

Why use a regular expression? Wouldn't it be easier to parse the date entered by the user into a DateTime then compare it to DateTime.Now to ensure that it is greater?

Here is an example:

Imports System

Class Test
    Private Shared Sub Main()
        Console.WriteLine(isFutureDate("5/16/1984"))
        Console.WriteLine(isFutureDate("5/16/2010"))
    End Sub
    
    Private Shared Function isFutureDate(ByVal candidate As String) As Boolean
        Dim future As DateTime
        
        DateTime.TryParse(candidate, future)
        
        Return future > DateTime.Now
    End Function
End Class
Andrew Hare
+2  A: 

I presume you want a regular expression so you can have it valid client side? You'd be better off with a custom validator, with both server side and client side code. You can set the client side code using the ClientValidationFunction property on the custom validator.

For the client side code you'd do embed something like the following script in your page (from the top of my head, not checked)

<script language="JavaScript">
<!--
  function CheckPrime(sender, args)
  {
    var currentDate = new Date();
    var enteredDate = new Date(args.Value);

    if (enteredDate > currentDate)
      args.IsValid = true;
    else
      args.IsValid = false;
  }
// -->
</script>

For server side you'd do the normal check you want in VB. The server check will always run, regardless of the client script results.

blowdart
how will i use this code for date textbox? can I have complete code the same
MKS
Not all clients are web clients.
Fredrik Mörk
Ah this is true, the regular expression requirement just made me think he's trying to use a standard regex ASP.NET validator
blowdart