ajax control toolkit calendar startdate=datetime.now FromDate To Date Validation - asp.net

how to validate FromDate and ToDat?
i'm using two textbox id-'FromDate', 'ToDate'
here is the client side code
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="FromDate">
</asp:CalendarExtender>
<asp:TextBox ID="FromDate" runat="server" width="158px"></asp:Textbox>
<asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="FromDate">
</asp:CalendarExtender>
<asp:TextBox ID="ToDate" runat="server" width="158px"></asp:Textbox>
here is the backend code
CalendarExtender1.startdate=datetime.now
CalendarExtender2.startdate=datetime.now
i'd lik to know, if i select fromdate(5/1/2012), the selection for 'Todate' should be equal or greater then fromdate,,,
it shouldn't lesserthen 'fromdate' lesserdays should be disable

you can work with a combination of the client and server side validation.
below alidation will work at client side to get a valid date from user.
<ajaxtoolkit:MaskedEditValidator ID="MaskedEditValidator3" runat="server" ControlExtender="MaskedEditExtender3" ControlToValidate="FromDate" EmptyValueMessage="Prosim vpišite Datum začetka." InvalidValueMessage="Please enter valid date." Display="None" TooltipMessage="Prosim vpišite Datum začetka." EmptyValueBlurredText="Empty" ValidationGroup="Offers" />
And then you can have a date validation method at server side to comapare from and to date. as below
int result = DateTime.Compare(Convert.ToDateTime(FromDate.Text), Convert.ToDateTime(ToDate.Text));
if (result < 0 || result == 0)
{
// user input passed
}
else
{
// error msg: "'To date' should be greater then 'From date'";
}

Related

check RadDateTimePicker value against current system date time value

How can I verify if the selected datetime value from the RadDateTimePicker is greater then the current system datetime value in asp.net using server side validaton.
<telerik:RadDateTimePicker runat="server" ID="orderDate" >
<Calendar ID="Calendar1" runat="server" EnableKeyboardNavigation="true">
</Calendar>
</telerik:RadDateTimePicker>
To validate it, you need to use CompareValidator or CustomValidator.
Please use following markup for datetime picker:
<telerik:RadDateTimePicker runat="server" ID="orderDate">
<Calendar ID="Calendar1" runat="server" EnableKeyboardNavigation="true">
</Calendar>
</telerik:RadDateTimePicker>
<asp:CustomValidator ID="cvOrderDate" runat="server" ControlToValidate="orderDate" ValidationGroup="vgSubmit" ErrorMessage="Please use correct date" OnServerValidate="cvOrderDate_ServerValidate"></asp:CustomValidator>
Code behind:
protected void cvOrderDate_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = orderDate.SelectedDate.Value > DateTime.Now;
}
There is also a demo of RadDateTimePicker validation here http://demos.telerik.com/aspnet-ajax/calendar/examples/datetimepicker/validation/defaultcs.aspx

Range Validator is not working in asp.net Minimum and Maximum Value

I have applied RangeValidator on TextBox. But it always shows me error : Invalid Range, though I have given minimum value 10 and maximum value 25. I want that user must not enter value whose length is less than 10 and greater than 25. I want that user can enter anything, so I have type="string" in RangeValidator. But it always gives me error message : Invalid Range.
<td>
<asp:TextBox ID="tbPassword" runat="server" MaxLength="25" type="password">
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server"
ControlToValidate="tbPassword" ForeColor="red" Display="Dynamic"
ErrorMessage="Password is required." SetFocusOnError="true">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvPassword" ControlToValidate="tbPassword"
ForeColor="red" Display="Dynamic" MinimumValue="10" MaximumValue="25"
SetFocusOnError="true" Type="String" runat="server"
ErrorMessage="Invalid Range">
</asp:RangeValidator>
</td>
For this you will need to use a CustomValidator control as suggested by Emad Mokhtar.
For server side validation, create an event like this.
protected void TextValidate(object source, ServerValidateEventArgs e)
{
e.IsValid = (e.Value.Length >= 10 && e.value.Length <= 25);
}
For client side validation, create a javascript function like this.
<script type="text/javascript">
function validateLength(oSrc, args){
args.IsValid = (args.Value.length >= 10 && args.Value.length <= 25);
}
</script>
Then in your aspx markup have the CustomValidator control like this.
<asp:Textbox id="tbPassword" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="customValidator" runat="server"
ControlToValidate = "tbPassword"
OnServerValidate="TextValidate"
ErrorMessage = "Password must be between 10 to 25 characters!"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
You can find more details here.
This validation can be implemented Using CustomValidator Control and apply your client and sever side validation, please find sample here.
I recently observed this cool feature, just use below attributes to asp control/html.
minLength="10" maxLength="1000"
as the attributes clearly states it allows minimum of 10 characters and maximum of 1000 characters.

how to get date from ajax calender in asp.net

The following is my .aspx code for ajax calender
<ajax:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBox1" runat="server">
</ajax:CalendarExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td><td>
and aspx.cs code is
string date = Request.Form[TextBox1.UniqueID];
comp.DATETIME = Convert.ToDateTime(date);
string value could not converted to datetime. how to convert this into datetime
<td>
Date Received From
</td>
<td >
<asp:TextBox ID="TxtSearchDate_Received_From" runat="server"></asp:TextBox>
<a runat="server" id="HrefDateReceivedFrom"></a>
<asp:CalendarExtender Format="dd-MM-yyyy" ID="CalendarExtender1" runat="server" TargetControlID="TxtSearchDate_Received_From"
PopupButtonID="HrefDateReceivedFrom" Enabled="True" />
</td>
Then use txtsearchDate_received_from control on the server side. This is a good practice as the use can view selected date in text box control.
Let me know if it works for you
Why haven't you tried just getting the .Text property value from the server control, like this?
string date = this.TextBox1.Text;
Then you can do your conversion to DateTime, like this:
comp.DATETIME = Convert.ToDateTime(date);

Asp.net validation error message to change labels text

I am using asp.net validation controls for validating user input. What i want is to change the label text with the error message generated by the validation control.
<asp:Label ID="Label1" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="nameB" Width="322px" Height="30px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="business" runat="server" ErrorMessage="Please tell us your name." ControlToValidate="nameBuisness" CssClass="errMsg" Display="Dynamic"></asp:RequiredFieldValidator>
Thank you.
One way is to handle the submit-button's OnClientClick-event and call a javascript function like:
<script type="text/javascript">
function displayValidationResult() {
// Do nothing if client validation is not available
if (typeof (Page_Validators) == "undefined") return;
var LblName = document.getElementById('LblName');
var RequiredName = document.getElementById('RequiredName');
ValidatorValidate(RequiredName);
if (!RequiredName.isvalid) {
LblName.innerText = RequiredName.errormessage;
}
}
</script>
<asp:Label ID="LblName" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="TxtNameBusiness" Width="322px" Height="30px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredName" ValidationGroup="business"
runat="server" ErrorMessage="Please tell us your name." ControlToValidate="TxtNameBusiness"
CssClass="errMsg" Display="None"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSumbit" runat="server" Text="Submit"
OnClientClick="displayValidationResult()" ValidationGroup="business" />
I've used some of the few ASP.NET client validation methods available. I've also renamed your controls to somewhat more meaningful and added a submit-button.
ASP.NET Validation in Depth
If your requirement is that you want to do this validation using the built-in ASP.Net validation controls, then you will need to use the ASP.Net CustomValidator control. This cannot be done using the ASP.Net RequiredFieldValidator control. To do the validation you specified, put a CustomValidator control on on your page so that the markup looks like this:
<asp:Label ID="Label1" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="nameB" Width="322px" Height="30px" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustValidator" runat="server" ControlToValidate="nameB"
ValidateEmptyText="true" ErrorMessage="Please tell us your name."
onservervalidate="CustValidator_ServerValidate" Display="none" >**</asp:CustomValidator>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" CausesValidation="true" />
You then need to write your custom validator. The server-side validation code looks like this:
protected void CustValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrWhiteSpace(args.Value))
{
Label1.Text = CustValidator.ErrorMessage;
args.IsValid = false;
}
else
{
Label1.Text = "Whats your name";
args.IsValid = true;
}
}
This custom validator will give you the behavior you desire.
In general when you use a CustomValidator control, you should do both server-side validation (for security) and client-side validation (for a better UI experience). To get client-side validation, you will need to write a client-side function in javascript and/or jQuery to do similar validation and then assign it to the ClientValidationFunction of the custom validator.
Further information on the CustomValidator control can be found in the MSDN documentation.

customvalidator always isvalid=true

I have a custom validator for checking if date enetred is valid. but it is always true that makes it not firing. I used to have a comaprevalidator and daterange but it won't work coz as page refreshes, it validates the date of birth and says invalid when it is. So I changed it to customvalidator hoping I will find luck.
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please enter MM/DD/YYYY format. Invalid Date 01/01/1901 to 12/31/9999"
ControlToValidate="txtAccusedDOB" SetFocusOnError="True" ClientValidationFunction="ServerValidation1" Display="Static">
<asp:Image ID="Image125" runat="server" ImageUrl="~/images/validatearrow.png" />
</asp:CustomValidator>
<ajaxToolkit:ValidatorCalloutExtender
runat="Server" ID="ValidatorCalloutExtender5" TargetControlID="CustomValidator1" Width="250px" HighlightCssClass="highlight"
CssClass="CustomCalloutStyle" PopupPosition="Right" WarningIconImageUrl="~/images/001_11.png"
CloseImageUrl="~/images/001_05.png" />
<asp:TextBox ID="txtAccusedDOB" runat="server" Style="text-transform: uppercase" onkeydown="TrapEnterKey()"
Width="70px" TabIndex="85">
</asp:TextBox>
here's the java script. it always shows the alert("test" + arguments.Value.toString()); The try catch is not working.
function ServerValidation1(source, arguments)
{
try{
var x= new Date();
x = Date.parse(arguments.Value.toString())
arguments.IsValid = true;
alert("test" + arguments.Value.toString());
}
catch(Error r)
{
arguments.IsValid = false;
alert("test OUt" + r.toString());
}
is there a simpler way to check if date entered is valid. I have been struggling on this for days already. thanks.

Resources