Category Archives: CompareValidator

[RESOLVED] Compare Validator-To Valid Date Greater then Start Date

i alredy defined culture too in page. but still it's showing enter valid date

 

my markup

<asp:CompareValidator ID="CompareValidator1" runat="server"
                    ControlToCompare="txt_startrate" ControlToValidate="txt_endrate"
                    Display="Dynamic" ErrorMessage="Enter a Valid Date"
                    Operator="GreaterThan"
                    Type="Date" ValidationGroup="ratemaster" CultureInvariantValues="True">*</asp:CompareValidator>


    #region Intializing Culture
    protected override void InitializeCulture()
    {
        Culture = "en-GB";
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
            base.InitializeCulture();
    }
    #endregion

Generate local resource and then try its will work.

 i m trying with javascript

what i m doing wrong let me know....?

<script type="text/javascript">
    function ValidDate(startdate,enddate)
    {
    var Sdate=document.getElementById(startdate).value;
    var Edate=document.getElementById(enddate).value;
     var startdate= new Date(Sdate);
     var enddate=new Date(Edate);
     if(startdate>enddate)
     {
     alert('Please Ensure End Date Should be Greater then Start Date')
     document.getElementById(endate)='';
     document.getElementById(enddate).focus();
     return false;

     }
     }
    </script>


        btn_RateSubmit.Attributes.Add("onclick", "ValidDate('" + txt_startrate.ClientID + "','" + txt_endrate.ClientID + "');");

Hi tan_vision_12_2010,

You can put the calender control aside of the textbox to ensure that the textbox have the same date format.

Or you use the RegularExpressionValidator control to valiadte it.

<asp:RegularExpressionValidator ID="regDate" runat="server" ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$" ControlToValidate="txtDate" SetFocusOnError="True" ></asp:RegularExpressionValidator>

Please check the following link:

http://forums.asp.net/t/1330456.aspx

http://regexlib.com/Search.aspx?k=Date&c=-1&m=-1&ps=20

I use custom validators for my date ranges. In the page I have:

<asp:CustomValidator ID="DateToCompareValidator" runat="server"
    ControlToValidate="DateTo"
    Text="*" ErrorMessage="Date To must be the same as, or later than Date From"
    ClientValidationFunction="compareDates" OnServerValidate="DateTo_CompareValidate" />

and the javascript function is:

    function compareDates(sender, args) {
        var startDate = '#<%=DateFrom.ClientID%>';
        var endDate = '#<%=DateTo.ClientID%>';

        var from = new Date(trim($(startDate).val()));
        var to = new Date(args.Value);
        var valid = (to >= from);
        args.IsValid = valid;
    }

And for the server-side validation:

    protected void DateTo_CompareValidate(object sender, ServerValidateEventArgs e)
    {
        if (!CheckDateTime(DateFrom.Text) || !CheckDateTime(e.Value))
        {
            e.IsValid = false;
        }
        else
        {
            DateTime from = DateTime.Parse(DateFrom.Text);
            DateTime to = DateTime.Parse(e.Value);

            e.IsValid = (to >= from);
        }
    }

 

Hi,

 

Instead of using Compare validator use jquery, please click on the link http://jqueryui.com/demos/datepicker/#date-range

jquery is easy to implement, on this link you can also get more feature which are on right panel of jquery page.

 

enjoy...

[RESOLVED] Req field validator like facebook

Hi a simple but a tricky one!!!

i need to display validation lik this.. for req field validator

Help needed..

gr8...

use the Validation controls...

CustomValidator control

and ValidationSummary..

You can style the ValidationSummary as you wish...

refer this

http://msdn.microsoft.com/en-us/library/aa479013.aspx

 

All the best

You can do this by using different asp.net validation controls along with Validation summary control. By using css you can achive same look and feel.

For the details of validation summary control please refer following url:

http://www.w3schools.com/aspnet/control_validationsummary.asp

 

<table>
                    <tr>
                        <td>Register</td>
                    </tr>
                    <tr>
                        <td>Firstname:</td>
                        <td>
                            <asp:TextBox ID="txtfirstname" runat="server" Width="200px" MaxLength="20"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                                ControlToValidate="txtfirstname" Display="Dynamic"
                                ErrorMessage="First name required" Text="*" ValidationGroup="reg"></asp:RequiredFieldValidator></td>
                    </tr>
                    <tr>
                        <td >Lastname:</td>
                        <td><asp:TextBox ID="txtlastname" runat="server" Width="200px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                                ControlToValidate="txtlastname" Display="Dynamic"
                                ErrorMessage="Last name required" Text="*" ValidationGroup="reg"></asp:RequiredFieldValidator></td>
                    </tr>
                   <tr>
                        <td >Email:</td>
                        <td><asp:TextBox ID="txtreg_email" runat="server" Width="200px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                                ControlToValidate="txtreg_email" Display="Dynamic" ErrorMessage="Email required"
                                ValidationGroup="reg" Text="*"></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" Text="*"
                                runat="server" ErrorMessage="Enter a valid email address."
                                ControlToValidate="txtreg_email" ForeColor="#FFFF99"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                                ValidationGroup="reg">*</asp:RegularExpressionValidator></td>
                       
                    </tr>
                   
                    <tr>
                        <td>Re-enter Email:</td>
                        <td><asp:TextBox ID="txtre_reg_email" runat="server"  MaxLength="10" Width="200px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" Text="*" ControlToValidate="txtre_reg_email" ></asp:RequiredFieldValidator>
                            <asp:CompareValidator ID="CompareValidator1" runat="server"
                                ErrorMessage="Your emails do not match. Please try again." Text="*"
                                ValidationGroup="reg" ControlToCompare="txtreg_email"
                                ControlToValidate="txtre_reg_email"></asp:CompareValidator>
                                    </td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><asp:TextBox ID="txtre_pwd" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
                                ControlToValidate="txtre_pwd"  Display="Dynamic" ErrorMessage="Password required"
                                ValidationGroup="reg" Text="*"></asp:RequiredFieldValidator></td>
                    </tr>
                   
                    <tr>
                        <td >Phone:</td>
                        <td><asp:TextBox ID="txtphone" runat="server" Width="200px"></asp:TextBox>
                         <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
                                ControlToValidate="txtphone" Text="*" Display="Dynamic" ErrorMessage="Phone required"
                                ValidationGroup="reg"></asp:RequiredFieldValidator></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><asp:Button ID="Button1" BackColor="#9C0C0A"
                                ForeColor="White" runat="server" ValidationGroup="reg" Text="Register"
                                Height="34px" Width="104px" onclick="Button1_Click" /></td>
                    </tr>
                </table>
                 <asp:ValidationSummary ID="ValidationSummary1" CssClass="validation" DisplayMode="List"
                            ValidationGroup="reg" runat="server" ForeColor="Black" />
Almost partially worked out... thanks hope it helps..

Image and video hosting by TinyPic

 

OOps.. Forgot insert css code..

.validation
{
	background-color:#ffebe8;
	border: 1px solid #dd3c10;
	color:#000;
	
}

[RESOLVED] CompareValidator checking if two textboxes are empty

1) Is there a way to use the CompareValidator to see if two ore three textboxes are empty? How?

2) Is there a way to use the CustomValidator client function to get the clientIDs of two textboxes and test it with javascript? I don't know how to configure the args of the client function to return more information? Is there any way of sending a commandargument string and use it through the args?

You cannot use the CompareValidator control for more than one control.  The only possible implementation for multiple controls to be validated is the CustomValidator.

1) You could add requiredfieldvalidators to make sure no field is empty.

 

2) In the client validation javascript function, you could do something like this:

var yourTextBox = $get('<%= yourTextBox.ClientID %>');

to retrieve a reference to yourTextBox which a server side control.

1) yes, of course; that is how I solved it before too. duh.

2) very cool, I will try that

[RESOLVED] problem of compare validator

 

hi

 

this is my comparevalidtor

<asp:CompareValidator id="Comparevalidator4" runat="server" ErrorMessage="the end date should greather than start date" Display="None"
				ControlToValidate="EDYM" Operator="LessThanEqual" Type="Date" ControlToCompare="STYM"></asp:CompareValidator>

when my STYM= '2010/01' and EDYM = '2011/02'

which still shows the error message

"the end date should greather than start date"

 

then I trid (switch ControlToValidate and ControlToCompare

<asp:CompareValidator id="Comparevalidator4" runat="server" ErrorMessage="the end date should greather than start date" Display="None"
ControlToValidate="STYM" Operator="LessThanEqual" Type="Date" ControlToCompare="EDYM"></asp:CompareValidator>

which still shows the error message

"the end date should greather than start date"

 

can you help me to found out where I did wrong?

thank you very much!!

I think the compare validator for dates uses the data format of te machine, and '2010/01' and '2011/02' aren't actually dates; they have no day component, and even if they did, that probably doesn't match your regional settings.

I'd probably use a custom validator for this; I'd add '/01' on the end then convert it to a date. Something like this:

<asp:CustomValidator ID="cv1" runat="server" ControlToValidate="STYM"
    ClientValidationFunction="compareYMDate" OnServerValidate="compareYMDate"
    Text="*" ErrorMessage="nope, try again" />

<script type="text/javascript">
    function compareYMDate(sender, args) {
        var stym = args.Value;
        var edym = document.getElementById('<%=EDYM.ClientID%>').value;

        var startDate = getDate(stym);
        var endDate = getDate(edym);
       
        args.IsValid = (endDate > startDate);
    }

    function getDate(dt) {
        var parts = dt.split("/");
        if (parts.length == 2)
            return new Date(parts[0], parts[1], 1);
        else
            return new Date();
    }
</script>   

Then the server event:

protected void compareYMDate(object sender, ServerValidateEventArgs e)
{
    var stym = e.Value;
    var edym = EDYM.Text;

    var startDate = getDate(stym);
    var endDate = getDate(edym);

    e.IsValid = (endDate > startDate);
}

DateTime getDate(string dt)
{
    DateTime date;

    if (DateTime.TryParse(dt + "/01", out date))
        return date;
    else
        return new DateTime();

}

[RESOLVED] Format Date in Asp:Textbox

Hi,

I have an asp:textbox that is a pulling a date from an sql database.  Within the database the format is similiar to this: 2011-3-8 00:00:00:000, however, when I bind the date field the format is Mar 3 2011.  I've tried adding DateFormatString and things similiar to this {0:d}, however, I've had no luck. Any help would be appreciated!

<asp:TextBox ID="date1" runat="server"   width="135px" Text='<%# Bind("date") %>' ></asp:TextBox>

Hi Skip10

This should help - has all options available to you in formatting dates;

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

HAve you tried like this...

Text='<%# Bind("date", "{MMM dd, yyy:d}") %>'

or something like that...for more format options - http://www.csharp-examples.net/string-format-datetime/

Thanks.

Thanks for the reply, the only solution I found to work is this:

<asp:TextBox ID="Date_Textbox" runat="server"   HtmlEncode="false" width="55px" Text='<%# Convert.ToDateTime(Eval("Date")).ToString("MM/dd/yyyy") %>'

However, it is affecting some existing validation that I have in place.  I'm trying to compare two asp:textbox dates, but in order to do that I had to make the format the same for the both of them ex. 3/9/2010 for Date1 and 4/2/2009 for Date2.  I'm using the comparevalidator and the validationcalloutextender. 

Any reasons why this date format would mess with my validation?

Thanks.

Surely if you're using a custom validator then the values of your textboxes would be compared in the code behind?

If this is the case then simply doing a date.parse() around both values before you compare them should yield the correct results.

Benjo

Nevermind.  It seems to working correctly, I guess the page was cached!

Thanks for the links and suggestions everyone, it really pointed me in the right direction :)