This is a migrated thread and some comments may be shown as answers.

[Solved] problem with required field validator

16 Answers 608 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 12 Mar 2008, 11:39 PM
Hi

I have a required field validator attached to a rad date picker to make sure the user enters a date. The problem is that if the user enters an invalid date like 32/4/2008 the required field validator is 'activated' even though it shouldnt be and my error message 'date required' is displayed. How can i stop invalid dates triggering the required field validator

thanks

16 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 13 Mar 2008, 01:25 PM
Hello andieje,

When you have DatePicker with RequiredFieldValidator, if date is not entered or is not valid, the error message of the validator will be displayed. You can try to implement your own custom validator to achieve your goal. Here is an example using such validator with DatePicker:
http://www.telerik.com/community/code-library/submission/b311D-baetkt.aspx

let us know if that helps.

Best wishes,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
andieje
Top achievements
Rank 1
answered on 14 Mar 2008, 08:05 PM

Hi

That doesnt make sense. Surely that is an error with the control? Invalid data entry into the date input should not trigger a required field validator because data is actually provided and hence data is not required.

In the client side DateSelected event, how would i detect if the date text box was empty?

thanks

0
Iana Tsolova
Telerik team
answered on 17 Mar 2008, 04:07 PM
Hello andieje,

RadDatePicker is designed so that its value is set only if it's valid. Thus if an invalid date is entered, it's SelectedDate is empty and the RequiredFieldValidator is fired. To check if RadDatePicker is empty client-side you need to use the isEmpty() function which return true if the DatePicker is empty.

Find more information about the client-side API of RadDatePicker in the following online resource:
http://www.telerik.com/help/radcontrols/prometheus/?cldClientSideAPI.html

I hope this information helps.

Best wishes,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
andieje
Top achievements
Rank 1
answered on 22 Mar 2008, 07:41 PM
thanks for your help
0
andieje
Top achievements
Rank 1
answered on 22 Mar 2008, 08:24 PM
Hi

I thought i had got this validator sorted but it woudl seem not. I have this code:

<

script type="text/javascript">

var invalidInput = false;

function DateInput_OnError(sender, args)

{

alert(

'error');

invalidInput =

true;

sender.innerHTML =

"Invalid date";

}

function CustomValidate(sender, args)

{

alert(

'custom validator');

var datePicker = $find("<%= txtDate.ClientID %>");

if (datePicker.isEmpty())

{

alert(

'empty');

sender.innerHTML =

"Date required";

args.IsValid =

false;

}

else if (invalidInput)

{

alert(

'invalid input');

sender.innerHTML =

"Invalid date";

args.IsValid =

false;

}

else args.IsValid = true;

}

Even if there is an invalid date in teh date text box, isEmpty always returns true. Why is this?

thanks

0
andieje
Top achievements
Rank 1
answered on 22 Mar 2008, 08:29 PM
hi
without using the client side error event as so

ClientEvents-OnError

="DateInput_OnError"

How coudl i detect on the client side if the date inside a datepicker is valid?

0
Iana Tsolova
Telerik team
answered on 24 Mar 2008, 05:51 PM
Hello andieje,

The value of RadDatePicker is not empty only if the value entered is valid. If the is no date entered or the date is invalid isEmpty() will return true. And only in case the date inside the DatePicker is valid isEmpty() will return false. You can use that check in your case with the custom validator.

I hope that helps.

Sincerely yours,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
andieje
Top achievements
Rank 1
answered on 24 Mar 2008, 08:08 PM
Hi

thanks for your reply but you did not answer this part of my question so I will repeat it here....


without using the client side error event as so

ClientEvents-OnError

="DateInput_OnError"

How coudl i detect on the client side if the date inside a datepicker is valid? I need to be able to discern between an empty date and an invalid date

thanks

0
Steve
Telerik team
answered on 25 Mar 2008, 08:25 AM
Hello andieje,

You cannot.. The logic of the control is such that there is one visible input (the one you see on the page) and one that is used for the actual value of the control. The second one is the input that is validated with the RequiredFieldValidator and if the entered date is invalid, it is wiped out from the "value input". As my colleague stated - isEmpty() will return false only when there is a valid date entered into the control.

Greetings,
Steve
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Phil
Top achievements
Rank 2
answered on 26 Jun 2009, 05:42 PM
Hi:
So, until one has a valid date, then the validator will not fire.  That is backwards.
Protected Sub radDatePickerCustomValidator(ByVal source As Object, ByVal eArgs As System.Web.UI.WebControls.ServerValidateEventArgs) 
    eArgs.IsValid = Not String.IsNullOrEmpty(eArgs.Value) 
End Sub 
 
The above never works, because it is only called when it is not empty/valid date.
Phil
0
Iana Tsolova
Telerik team
answered on 29 Jun 2009, 11:21 AM
Hello Phil,

Could you please try sending us a runnable page illustrating your scenario and the issues you are facing? I will debug it locally and try finding a proper solution for you.

Kind regards,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Phil
Top achievements
Rank 2
answered on 30 Jun 2009, 02:10 AM
Hi:
This code worked fine on my laptop.  I am going to try it at work.
<%@ Page Language="C#" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
    { 
        args.IsValid = !String.IsNullOrEmpty(args.Value); 
    } 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title></title
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
        </telerik:RadScriptManager> 
        <asp:Button ID="Button1" runat="server" Text="Button" /> 
        <telerik:RadDatePicker ID="RadDatePicker1" runat="server"
        </telerik:RadDatePicker> 
    </div> 
    <asp:CustomValidator ID="CustomValidator1" runat="server"  
        ErrorMessage="CustomValidator"  
        onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator> 
    </form> 
</body> 
</html> 
 

0
Phil
Top achievements
Rank 2
answered on 01 Jul 2009, 12:34 AM
Hi:
It is working different from my original experience.  My code does call the event much better than my previous experience.
BUT, the value does not come through in 'args.Value'.  'args.Value' seems to always return String.Empty.
Phil
0
Iana Tsolova
Telerik team
answered on 02 Jul 2009, 02:19 PM
Hello Phil,

Indeed, it seems you are rightfor the args.Valueproperty. However it behaves same way if you replace the RadDatePicker with ASP:TextBox control.
Therefore I suggest that you try modifying the server event handler as below:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)    
{  
    args.IsValid = !String.IsNullOrEmpty(RadDatePicker1.SelectedDate.ToString());    
}  

Kind regards,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Phil
Top achievements
Rank 2
answered on 02 Jul 2009, 09:56 PM
Sorry coding to fast, validator need a control to validate:
<asp:CustomValidator ID="CustomValidator1" runat="server"  
    ErrorMessage="CustomValidator" ControlToValidate="RadDatePicker1" 
    onservervalidate="CustomValidator1_ServerValidate" > 
</asp:CustomValidator> 
 
Now it is behaving poorly.  It is not called if it is empty, and only called if valid date.


0
Phil
Top achievements
Rank 2
answered on 03 Jul 2009, 04:19 AM
Custom validator does have a property of ValidateEmptyText ... the required field and my 'week day' custom validators work.
<%@ Page Language="C#" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
    DayOfWeek _dow = DateTime.Parse(args.Value).DayOfWeek; 
    args.IsValid = !(_dow == DayOfWeek.Sunday || _dow == DayOfWeek.Saturday); 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title> rad date picker </title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
        </telerik:RadScriptManager> 
        <asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="true" /> 
        <telerik:RadDatePicker ID="RadDatePicker1" runat="server"
        </telerik:RadDatePicker> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" 
            ErrorMessage="Date is required" ControlToValidate="RadDatePicker1" > 
            </asp:RequiredFieldValidator> 
        <asp:CustomValidator ID="CustomValidator1" runat="server" Text="*" 
            ErrorMessage="Date must be a week day." ControlToValidate="RadDatePicker1" 
            onservervalidate="CustomValidator1_ServerValidate"
        </asp:CustomValidator> 
        <asp:ValidationSummary ID="rdpValidationSummary" runat="server" /> 
    </div> 
    </form> 
</body> 
</html> 
 

Tags
Calendar
Asked by
andieje
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
andieje
Top achievements
Rank 1
Steve
Telerik team
Phil
Top achievements
Rank 2
Share this question
or