[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

dd/mm/yyyy format only

Posted on 2004-11-27
23
Medium Priority
?
1,587 Views
Last Modified: 2012-06-27
hi,
how do we restrict the user to enter a valid date in the format dd/mm/yyyy only (TEXTBOX ENTRY)
0
Comment
Question by:sainavya1215
  • 12
  • 8
  • 2
  • +1
23 Comments
 
LVL 16

Expert Comment

by:muzzy2003
ID: 12686238
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.
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12686318
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



0
 

Author Comment

by:sainavya1215
ID: 12688833
ok fine but how do we code this..
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:mani_sai
ID: 12690471
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;
}
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12690497
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;

}


0
 

Author Comment

by:sainavya1215
ID: 12693334
I am using Windowsforms and vb.net..its pure application development not web
0
 
LVL 4

Expert Comment

by:Jigit
ID: 12695112
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

0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12696136
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
0
 
LVL 4

Expert Comment

by:Jigit
ID: 12696303
mani_sai, your regular expression ignores days number in months, for example, december has 31 days. It also ignores leap years...
0
 
LVL 8

Accepted Solution

by:
mani_sai earned 1000 total points
ID: 12696994
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
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12697040
Sorry i forget to mention Importing namespace:

Imports System.Text.RegularExpressions


Thanks
0
 

Author Comment

by:sainavya1215
ID: 12705371
Excellent thanks a lot for that.
0
 

Author Comment

by:sainavya1215
ID: 12743937
Hi mani,
Using your code how could we make sure that date valid is not > Today
Pls let me know..Thanks in advance
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12745134
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
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12745219
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
0
 

Author Comment

by:sainavya1215
ID: 12749525
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)

0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12754838
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
0
 

Author Comment

by:sainavya1215
ID: 12758044
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
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12758156
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
0
 

Author Comment

by:sainavya1215
ID: 12763840
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
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12763914
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


0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12763964
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)
0
 

Author Comment

by:sainavya1215
ID: 12767255
Excellent..............Your help was very useful..........Thx once again
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question