hi guys, I have a textbox for entering date.Textbox should allow only dd/MM/yyyy format.How can i check it using javascript
A:
Javscript Code.
var RegEx = "^((((([13578])|(1[0-2]))[\-\/\s]?(([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-\/\s]?(([1-9])|([1-2][0-9])|(30)))|(2[\-\/\s]?(([1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s((([1-9])|(1[02]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$"
Check these URL http://blog.stevenlevithan.com/archives/date-time-format
http://dotnetdiary.wordpress.com/2009/01/21/datetime-validation-using-javascript/
Muhammad Akhtar
2009-10-06 04:45:05
+1
A:
If you're using WebForms, just use a CompareValidator:
<asp:CompareValidator runat="server" ControlToValidate="txtInput" Type="Date"
Operator="DataTypeCheck" ErrorMessage="That's not a valid date!" />
Mark Brackett
2009-10-06 04:51:11
+1
A:
You're using dd/mm/yyyy to validate dates -- you can use the JavaScript date object to validate dates but its trickier in this case:
var o = document.form1.date1; // o is a reference to the textbox
var r = /^(\d+)\/(\d+)\/(\d+)$/.exec( o.value ); // extract day, month and year
if ( ! r )
{
// invalid date -- pattern matching failed
}
var d = parseInt( r[ 1 ] );
var m = parseInt( r[ 2 ] );
var y = parseInt( r[ 3 ] );
var c = new Date( y, m - 1, d ); // month is zero based but year and day are not
if ( isNaN( c ) )
{
// invalid date -- javascript could not make a date out of the three numbers
}
Salman A
2009-10-06 05:01:26
You should use the radix argument of the parseInt function, you could have problems *specially* with *date part* numbers which have preceding zeros, for example: `parseInt('08') == 0`. The preceding zero indicates to parseInt that the number being parsed is an octal number, you should use `parseInt(n, 10)`.
CMS
2009-10-06 05:38:51