Link to home
Start Free TrialLog in
Avatar of sainavya1215
sainavya1215

asked on

dd/mm/yyyy format only

hi,
how do we restrict the user to enter a valid date in the format dd/mm/yyyy only (TEXTBOX ENTRY)
Avatar of muzzy2003
muzzy2003

You can use a regular expression validator to enforce nn/nn/nnnn format - in fact, you can use a regular expression validator to enforce nn/[0 or 1]n/nnnn format (these aren't regular expressions, they're written this way for clarity). If you want more complete validation, your best bet is to write a CustomValidator to do this. In the validation function, turn their input round to be in yyyy-mm-dd format and do a Date.Parse inside a Try ... Catch block.
You can try this:

To match a date in mm/dd/yyyy format,
the regular expression to use:   (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d .

For dd-mm-yyyy format,

use:    (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d .

Thanks



Avatar of sainavya1215

ASKER

ok fine but how do we code this..
Try this function:

function DateComponents(dateStr, format) {

var results = new Array();
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat);

if (matchArray == null) return null;
// parse date into variables
if (format.charAt(0)=="d"){ //format=dd/mm
results[0] = matchArray[1];
results[1] = matchArray[3];
} else {
results[1] = matchArray[1];
results[0] = matchArray[3]; }
results[2] = matchArray[4];
return results;
}
or you can try with this  javascript function:

function validate(str) {
    var success = true;

    var pattern = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // a string of letters, plus comma or hyphen

     if (!pattern.test(str)) {

         success = false;

     }

    return success;

}


I am using Windowsforms and vb.net..its pure application development not web
The following functions checks date for valid format dd/mm/yyyy using regular expressions. Regular expression was taken from http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=5&categoryId=5

private bool IsValidDate( string strDate )
{

System.Text.RegularExpressions.Regex expr= new Regex( @"^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$", RegexOptions.IgnoreCase | RegexOptions.Compiled  );

return expr.IsMatch( strDate );

}

HTH,
Jigit

Here is windows / vb.net code:
Tested the code works fine for dd/mm/yyyy format.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim text As String = TextBox1.Text

       Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"

        ' Compile the regular expression.
        Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)

        ' Match the regular expression pattern against a text string.
        Dim m As Match = r.Match(text)

        If m.Success Then

            MsgBox("Match")

        Else
            MsgBox("Match Not Found")
        End If

    End Sub


Thanks
mani_sai, your regular expression ignores days number in months, for example, december has 31 days. It also ignores leap years...
ASKER CERTIFIED SOLUTION
Avatar of Mani Pazhana
Mani Pazhana
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry i forget to mention Importing namespace:

Imports System.Text.RegularExpressions


Thanks
Excellent thanks a lot for that.
Hi mani,
Using your code how could we make sure that date valid is not > Today
Pls let me know..Thanks in advance
Try this:

 Dim text As String = TextBox1.Text

        Dim pat As String = "((((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))"



        'Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"

        ' Compile the regular expression.
        Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)

        ' Match the regular expression pattern against a text string.
        Dim m As Match = r.Match(text)

        If m.Success Then

            MsgBox("Match")



        Else
            MsgBox("Match Not Found")
        End If

    End Sub
Sorry try this code:

Try this:

  Dim InputDate As Date = TextBox1.Text    'input date to check
  Dim TodayDate As Date = System.DateTime.Today   'todays date

'checking for input date is greater than todays date.

   If (DateTime.Compare(InputDate, TodayDate) > 0) Then
                MsgBox("Input Date is not Valid")  'if yes it is not valid date
            Else
                MsgBox("Input Date is Valid")  'if no it is a valid date
            End If

        Else
            MsgBox("Match Not Found")
        End If
Hi Mani,

I tried out the posted code. I first used the Previous posted code to check if the  date is in dd/mm/yyyy (ie RegularExpressions code)

then I tried checking if date > Today . I entered    08/12/2004 (It gives valid date but doesnot show that its greater than today)

OK, my previous will work good gor date in mm/dd/yyyy format.

I am sending you the code for dd/mm/yyyy format.


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim SubmitDate As DateTime = New DateTime

        Dim culture As System.Globalization.CultureInfo
        culture = New System.Globalization.CultureInfo("en-GB", True)
        If TextBox1.Text.ToString() <> "" Then

            SubmitDate = DateTime.Parse(TextBox1.Text.ToString(), culture, System.Globalization.DateTimeStyles.NoCurrentDateDefault)

            If (SubmitDate > DateTime.Now) Then
                MsgBox("The Submission Date is Greater than Current Date")
            Else
                MsgBox("Date is Valid")
            End If

        End If

    End Sub
Ok Mani sai .......

Can i avoid using this code then for dd.mm.yyy format
Dim text As String = TextBox1.Text

        Dim pat As String = "((((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))"



        'Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"

        ' Compile the regular expression.
        Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)

        ' Match the regular expression pattern against a text string.
        Dim m As Match = r.Match(text)

        If m.Success Then

            MsgBox("Match")



        Else
            MsgBox("Match Not Found")
        End If

    End Sub
Yes you can .

Instead you can use this, it takes care of format.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim SubmitDate As DateTime = New DateTime

        Dim culture As System.Globalization.CultureInfo
        culture = New System.Globalization.CultureInfo("en-GB", True)
        If TextBox1.Text.ToString() <> "" Then

            SubmitDate = DateTime.Parse(TextBox1.Text.ToString(), culture, System.Globalization.DateTimeStyles.NoCurrentDateDefault)

            If (SubmitDate > DateTime.Now) Then
                MsgBox("The Submission Date is Greater than Current Date")
            Else
                MsgBox("Date is Valid")
            End If

        End If

    End Sub
Hi mani sai,

I enerted  08/11/2004 in textbox to check the validation you cannot enter date > Today

if u check out the variable submit date it shows as 11/08/2004 and it returns valid date instead of displaying the error
Ok, You need to use regular expression to check the date format in (dd/mm/yyyy).

Next you do validation to check date entered is not greater than today.

check this code below:

-----------------------------------------------------------------------------------------------------

Dim text As String = TextBox1.Text

        Dim pat As String = "((((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))"



        'Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"

        ' Compile the regular expression.
        Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)

        ' Match the regular expression pattern against a text string.
        Dim m As Match = r.Match(text)

        If m.Success Then

           Dim SubmitDate As DateTime = New DateTime

           Dim culture As System.Globalization.CultureInfo
           culture = New System.Globalization.CultureInfo("en-GB", True)
           If TextBox1.Text.ToString() <> "" Then

            SubmitDate = DateTime.Parse(TextBox1.Text.ToString(), culture,      
System.Globalization.DateTimeStyles.NoCurrentDateDefault)

            If (SubmitDate > DateTime.Now) Then
                MsgBox("The Submission Date is Greater than Current Date")
            Else
                MsgBox("Date is Valid")
            End If

        End If



        Else
            MsgBox("Match Not Found")
        End If


In the above code you can remove the line of code shown below:

If TextBox1.Text.ToString() <> "" Then


End if


no need to check because already you are validating with regular expression.(textbox1.text)
Excellent..............Your help was very useful..........Thx once again