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)
sainavya1215Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muzzy2003Commented:
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
mani_saiCommented:
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
sainavya1215Author Commented:
ok fine but how do we code this..
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

mani_saiCommented:
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
mani_saiCommented:
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
sainavya1215Author Commented:
I am using Windowsforms and vb.net..its pure application development not web
0
JigitCommented:
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
mani_saiCommented:
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
JigitCommented:
mani_sai, your regular expression ignores days number in months, for example, december has 31 days. It also ignores leap years...
0
mani_saiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mani_saiCommented:
Sorry i forget to mention Importing namespace:

Imports System.Text.RegularExpressions


Thanks
0
sainavya1215Author Commented:
Excellent thanks a lot for that.
0
sainavya1215Author Commented:
Hi mani,
Using your code how could we make sure that date valid is not > Today
Pls let me know..Thanks in advance
0
mani_saiCommented:
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
mani_saiCommented:
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
sainavya1215Author Commented:
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
mani_saiCommented:
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
sainavya1215Author Commented:
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
mani_saiCommented:
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
sainavya1215Author Commented:
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
mani_saiCommented:
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
mani_saiCommented:
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
sainavya1215Author Commented:
Excellent..............Your help was very useful..........Thx once again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.