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)
how do we restrict the user to enter a valid date in the format dd/mm/yyyy only (TEXTBOX ENTRY)
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
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]
For dd-mm-yyyy format,
use: (0[1-9]|[12][0-9]|3[01])[-
Thanks
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;
}
function DateComponents(dateStr, format) {
var results = new Array();
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})
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;
}
function validate(str) {
var success = true;
var pattern = /^(\d{1,2})(\/|-)(\d{1,2})
if (!pattern.test(str)) {
success = false;
}
return success;
}
ASKER
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.RegularExpress ions.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[1 3456789]|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\/0 2\/((1[6-9 ]|[2-9]\d) (0[48]|[24 68][048]|[ 13579][26] )|((16|[24 68][048]|[ 3579][26]) 00))))$", RegexOptions.IgnoreCase | RegexOptions.Compiled );
return expr.IsMatch( strDate );
}
HTH,
Jigit
private bool IsValidDate( string strDate )
{
System.Text.RegularExpress
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
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])[
' 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry i forget to mention Importing namespace:
Imports System.Text.RegularExpress ions
Thanks
Imports System.Text.RegularExpress
Thanks
ASKER
Excellent thanks a lot for that.
ASKER
Hi mani,
Using your code how could we make sure that date valid is not > Today
Pls let me know..Thanks in advance
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[13 456789]|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]|[246 8][048]|[1 3579][26]) |((16|[246 8][048]|[3 579][26])0 0)))))"
'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
Dim text As String = TextBox1.Text
Dim pat As String = "((((0[1-9]|[12]\d|3[01])\
'Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[
' 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(InputDat e, 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
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(InputDat
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
ASKER
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)
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.Cultu reInfo
culture = New System.Globalization.Cultu reInfo("en -GB", True)
If TextBox1.Text.ToString() <> "" Then
SubmitDate = DateTime.Parse(TextBox1.Te xt.ToStrin g(), culture, System.Globalization.DateT imeStyles. NoCurrentD ateDefault )
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
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.Cultu
culture = New System.Globalization.Cultu
If TextBox1.Text.ToString() <> "" Then
SubmitDate = DateTime.Parse(TextBox1.Te
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
ASKER
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[13 456789]|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]|[246 8][048]|[1 3579][26]) |((16|[246 8][048]|[3 579][26])0 0)))))"
'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
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])\
'Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[
' 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.Cultu reInfo
culture = New System.Globalization.Cultu reInfo("en -GB", True)
If TextBox1.Text.ToString() <> "" Then
SubmitDate = DateTime.Parse(TextBox1.Te xt.ToStrin g(), culture, System.Globalization.DateT imeStyles. NoCurrentD ateDefault )
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
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.Cultu
culture = New System.Globalization.Cultu
If TextBox1.Text.ToString() <> "" Then
SubmitDate = DateTime.Parse(TextBox1.Te
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
ASKER
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
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[13 456789]|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]|[246 8][048]|[1 3579][26]) |((16|[246 8][048]|[3 579][26])0 0)))))"
'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.Cultu reInfo
culture = New System.Globalization.Cultu reInfo("en -GB", True)
If TextBox1.Text.ToString() <> "" Then
SubmitDate = DateTime.Parse(TextBox1.Te xt.ToStrin g(), culture,
System.Globalization.DateT imeStyles. NoCurrentD ateDefault )
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
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])\
'Dim pat As String = "(0[1-9]|[12][0-9]|3[01])[
' 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.Cultu
culture = New System.Globalization.Cultu
If TextBox1.Text.ToString() <> "" Then
SubmitDate = DateTime.Parse(TextBox1.Te
System.Globalization.DateT
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)
If TextBox1.Text.ToString() <> "" Then
End if
no need to check because already you are validating with regular expression.(textbox1.text)
ASKER
Excellent..............You r help was very useful..........Thx once again