I have a RadGrid with GridDateTimeColumn as below.
<rad:GridDateTimeColumn DataField="Date1" UniqueName="Date1" EditFormColumnIndex="0" DataFormatString="{0:d}" >
</rad:GridDateTimeColumn>
How can i check whether the entered date by user in the dateinput textbox is greater than or equal to todays date?Note user enters the date value instead of selecting from the datepicker
How can i have validations to the column during inserting and updating a record?
Regards
Jack
17 Answers, 1 is accepted
You can use CompareValidator in order to validate the entered date. Similar to this:
protected void RadGrid_ItemCreated(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridEditableItem && e.Item.IsInEditMode) |
{ |
GridDateTimeColumnEditor columnEditor = |
((GridEditableItem) e.Item).EditManager.GetColumnEditor("Date1") as GridDateTimeColumnEditor; |
TableCell cell = (TableCell) columnEditor.PickerControl.Parent; |
CompareValidator compareValidator = new CompareValidator(); |
compareValidator.ID = "compareValidator"; |
compareValidator.ControlToValidate = columnEditor.PickerControl.ID; |
compareValidator.Operator = ValidationCompareOperator.GreaterThanEqual; |
compareValidator.Type = ValidationDataType.Date; |
compareValidator.ErrorMessage = "*"; |
compareValidator.ValueToCompare = DateTime.Now.ToShortDateString(); |
cell.Controls.Add(compareValidator); |
} |
} |
Information about how to add validators to RadGrid columns can be found in this help article as in this live demo here.
Greetings,
Rosen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
<
telerik:GridDateTimeColumn
UniqueName="ExpiryDateTime"
DataField="ExpiryDateTime"
DataFormatString="{0:MM/dd/yyyy}"
HeaderText="Expiration"
PickerType="DatePicker"
HeaderStyle-Width="18%"
/>
is all I have right now inside the grid.
Thanks
Please review the following online resources on RadGrid validation and and make sure you are following the description and samples provided there in order to achieve your goal:
http://www.telerik.com/help/aspnet-ajax/grdvalidation.html
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/validation/defaultcs.aspx
Give it a try and let me know if further issues arise.
Sincerely yours,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Is there any way to validate the
<telerik:GridDateTimeColumn UniqueName="fromDate" PickerType="DatePicker" HeaderText="From Date"
DataField="fromDate"><ItemStyle Width="80px" /></telerik:GridDateTimeColumn>
<telerik:GridDateTimeColumn UniqueName="toDate" PickerType="DatePicker" HeaderText="To Date"
DataField="toDate"><ItemStyle Width="80px" /> </telerik:GridDateTimeColumn>
GridDateTimeColumn to Require Field Validation and Compare Validation. (for example a datetime field generates error if the user enters something else).
Thanks
Krish
You can add the desired validators to the edit fields in RadGrid by handling the grid ItemCreated event as shown here.
All the best,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Try using the GridDateTimeColumn for your purpose. You need to set the PickerType to TimePicker.
For more information on the grid column types, check this online demo.
All the best,
Iana
the Telerik team
While adding a new record, When no value is selected in datetimecolumn. It is defaulty taking some value.(i think 01/01/0001 ).
and it is throwing error and the insert form is going. SO i had to reenter the values.
I want to display error saying to pick some datetime value.
Any help pls.
Thank you
You can add a RequiredFieldValidator in addition to the CompareValidator.
Sincerely yours,
Iana
the Telerik team
I looked at the online demo. However, the DateTime Column does display the warning icon but it still allows one to save the record if I typed invalid date say for example the letters "abcd" in the date input field manually. I want the update to display an error message and not allow the record to be saved. How to achieve this?
thanks
milind
You should use a validator in addition to the date picker control. Can you confirm you tried it?
Kind regards,
Iana
the Telerik team
I have tried using the validator like one of the examples. However, the validator doesn't cause the exception for the update button and allows me to save a blank date.
Thanks
milind
Can you share a runnable code so I can check it out?
Best wishes,
Iana
the Telerik team
hi Lana,
I followed the example demo http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx
I just added the following code for my example. It seems the problem is that "ControlToValidate" is PickerControl which doesn't fire validation
However, if I set ControlToValidate = columnEditor.PickerControl.DateInput.ID I get runtime error. It still allows me to save the date as NULL when I click on the update button.
If you inspect the example in the link above, it allows you to save invalid date as NULL even though the picker control flags it as invalid.
thanks
milind
protected void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridDateTimeColumnEditor columnEditor =
((GridEditableItem) e.Item).EditManager.GetColumnEditor("expirationDate") as GridDateTimeColumnEditor;
TableCell cell = (TableCell) columnEditor.PickerControl.Parent;
RegularExpressionValidator validator = new RegularExpressionValidator();
validator.ID = "expirationDateValidator";
validator.ControlToValidate = columnEditor.PickerControl.ID;
validator.ErrorMessage = "Invalid date";
// yyyy-mm-dd format
validator.ValidationExpression = @"(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])";
cell.Controls.Add(validator);
}
}
Before setting the ControlToValidate property, try setting the ID of the PickerControl as this is done here for the TextBoxControl.
Regards,
Iana
the Telerik team
The example you described works for RequiredFieldValidator but doesn't work for date validation using RegularExpressionValidator.
thanks
milind
Please find the attached sample and let me know if it works and if I missed something out.
Best wishes,
Iana
the Telerik team