Search This Blog

Friday 25 February 2011

DateTime Control in .net and Javascript validation for the same

Recently I had an requirment of validation or checking DatePicker for null or empty using Javascript.

My DatePicker was created on runtime using below code.

DateTimeControl StartDate = new DateTimeControl();


First I tried writing on Submit button, [this is wriitrn in .net and Javascript is called inside .cs using onclick event of button]

lbl_Submit.Attributes.Add("onClick", function checkStartDate() {if(document.getElementById(" + StartDate.ClientID + ").value==''){msg = msg + 'Please Select Start Date'; alert('msg'); return false;};};return checkStartDate(););

Here StartDate.ClientDate gives ID of DateTimeControl , but this validation was not working . ;(

Then I found that, DateTimeControl is made up of two parts, one is Textbox and other part is calander.

So if you want to check for null value ,you need to take ClientId of Textbox instead of entire control.

This can be done as shown below.

StartDate.Control[0].ClientID // This works fine.

Here Control[0] ---> indicates index of Textbox

So final code would be

lbl_Submit.Attributes.Add("onClick", function checkStartDate() {if(document.getElementById(" + StartDate.Control[0].ClientID+ ").value==''){msg = msg + 'Please Select Start Date'; alert('msg'); return false;};};return checkStartDate(););





Njoy!!!

1 comment: