Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

Please suggest me about that,

I have use Callender control for textbox,but when i insert a date 2013/02/29 in textbox then its through error,

How can i tackled this situation?
Please provide me solution /or suggest.

thanks in advanced
Posted
Comments
Muralikrishna8811 19-Apr-12 10:28am    
Hi can u specify which date format which that callender control accepting

C#
protected bool CheckDate(String date)
       {
           try
           {
               DateTime dt = DateTime.Parse(date);
               return true;
           }
           catch
           {
               return false;
           }
       }

by using the function You can clear this problem

Eg.
if(CheckDate())
//insert function
else
//Throw error msg

if you want to use by script means try with this,
C#
<script type="text/javascript">
function checkdate()
{
   var validformat=/^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity
   var returnval=false;
   var inputvalue=document.getElementById("txtdate").value;
   if (!validformat.test(inputvalue))
   {
      alert("Invalid Date Format. Please correct and submit again.")
   }
   else
   { 
      //Detailed check for valid date ranges
      var monthfield=inputvalue.split("/")[1]
      var dayfield=inputvalue.split("/")[0]
      var yearfield=inputvalue.split("/")[2]
      var dayobj = new Date(yearfield, monthfield-1, dayfield)

      if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
      {
         alert("Invalid Day, Month, or Year range detected. Please correct and submit again.");
      }
      else
      {
         returnval=true;
      }
   }
   if (returnval==false) 
   {
      input.select();
      return returnval;
   }
</script>


by calling
XML
<asp:TextBox ID="txtdate" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="btnSave" runat="server" OnClientClick="return checkdate(this.txtdate)"  onclick="BtnSave_Click"
                                    Text="Save"   />


Regards,
Pal
 
Share this answer
 
v4
Comments
madhuri@mumbai 19-Apr-12 10:00am    
can i use this in javascript,then how
palraj001 19-Apr-12 12:25pm    
hi refer the updated answer..
abhishekagrwl25 19-Apr-12 22:14pm    
if i have to validate two textboxes on one button click..then what shuold i do..??
Hi,

dont allow user to enter date manually in textbox, force user to use calender
then yourur problem will solved.
to restrict write following line at pageload event

txtDate.Attributes["onkeypress"]="return false";

Best Luck
 
Share this answer
 
v4
Comments
madhuri@mumbai 19-Apr-12 9:54am    
how can i do this, by set ReadOnly property of textbox ,
I have applied this but it doesn't take value of textbox which i have set by calender.
Muralikrishna8811 19-Apr-12 10:34am    
If you want to avoid user entry thn just add disabled="disabled" to textbox
fjdiewornncalwe 19-Apr-12 21:49pm    
Please avoid text speak in your answers.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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