Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a textbox for date and in the textbox if I enter string I want it to display an alert msg.
Posted
Updated 14-Dec-11 22:48pm
v2
Comments
sriman.ch 15-Dec-11 5:23am    
I will give you javascript function shortly....

function date(){

var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.");
}


actually this one is giving alert, but my date format is mon-day-year

so if i give correct date lik 12-11-2003,it shows error msg, because this one (/^\d{2}\/\d{2}\/\d{4}$/) is checking lik this 12/11/2003.how can i get lik mon-day-year .can i change the - instead of /
 
Share this answer
 
v2
Comments
Dalek Dave 15-Dec-11 4:48am    
Good answer
 
Share this answer
 
Try this code

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Registration Form
</title>
<script type="text/javascript">
function check()
{
var ck_date = /^(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01]\-[0-9]{4})/;
d=document.form;
if (document.form.date_s.value.search(ck_date)==-1)
{
alert("Please only valid date in (mm-dd-yyyy)");d.date_s.value='';d.date_s.focus();return false
}
else
{
alert("Date validated OK.");d.date_s.value='';d.date_s.focus();return false}
}
</script>
</head>
<body>
<form style='margin-left:340px' name='form' action=" " method="POST" onsubmit="return check()">
<INPUT maxLength="12" type="text" name="date_s" size="12">
<INPUT TYPE="SUBMIT" value="Submit Date">
<INPUT TYPE="RESET" value="reset">
</form>
    </body>
</html>
 
Share this answer
 
v2
Comments
Dalek Dave 15-Dec-11 4:49am    
Excellent Answer.
Balakrishnan Dhinakaran 15-Dec-11 4:50am    
Thank you
priya from Madras 15-Dec-11 5:42am    
ya but in buttonclick event , its not working , help me
Hi
You can regular expression validator

XML
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please enter a valid date..."
                                    Text="*" ForeColor="Red" ControlToValidate="txtHDate" ValidationGroup="modal"
                                    ValidationExpression="^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[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))))$"></asp:RegularExpressionValidator>



Above is for date fromat in dd-mm-yyyy

Hope this helps
 
Share this answer
 
Hi
Have a look on the following javascript function

C#
function isDate(dateStr)
{
var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)((\d{1,2})|(\d{4}))$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null)
{
alert("Please enter a valid date...");
return false;
}
month = matchArray[3];
day = matchArray[1];
year = matchArray[5];
if (month < 1 || month > 12)
{
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31)
{
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
alert("Month "+month+" doesn`t have 31 days!");
return false;
}
if (month == 2)
          {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap))
{
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}


return true;
}
 
Share this answer
 
Finally i used lik this ,its working fine Thanks all



var validformat = /^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/;  //Basic check for format validity

         if (!validformat.test(Textbox1)) {
             alert("StartDate and EndDate has to be enter in correct format.");
             return false;
         }
 
Share this answer
 
v2
check following link
Date Validation in JavaScript[^]
 
Share this answer
 
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Registration Form
</title>
<script type="text/javascript">
function check()
{
var ck_date = /^(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01]\-[0-9]{4})/;
d=document.form;
if (document.form.date_s.value.search(ck_date)==-1)
{
alert("Please only valid date in (mm-dd-yyyy)");d.date_s.value='';d.date_s.focus();return false
}
else
{
alert("Date validated OK.");d.date_s.value='';d.date_s.focus();return false}
}
</script>
</head>
<body>
<form style='margin-left:340px' name='form' action=" " method="POST">
<INPUT maxLength="12" type="text" name="date_s" size="12" onblur="return check()">
<INPUT TYPE="SUBMIT" value="Submit Date">
<INPUT TYPE="RESET" value="reset">
</form>
    </body>
</html>


I have updated check this I just call the Javascript function during onblur event of the textbox so it will validate without clicking the button..:-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900