We help IT Professionals succeed at work.

How to put compare validator in 2 textboxes.

searchsanjaysharma
searchsanjaysharma used Ask the Experts™
on
I have 2 textboxes, txtfromdate and txttodate
txttodate should be > txtfromdate.
I want two validations.
1.todate should be more than txtfromdate.
2.both the dates are mandatory.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Try:

<asp:CompareValidator id="Compare1" 
           ControlToValidate="txttodate" 
           ControlToCompare="txtfromdate"
           Operation="GreaterThan"
           Type="Date" 
           runat="server"/>

<asp:RequiredFieldValidator id="RequiredFieldValidator1"
           ControlToValidate="txttodate"
           Display="Static"
           ErrorMessage="* To Date is required"
           runat="server"/> 

<asp:RequiredFieldValidator id="RequiredFieldValidator2"
           ControlToValidate="txtfromdate"
           Display="Static"
           ErrorMessage="* From Date is required"
           runat="server"/> 

Open in new window

Author

Commented:
not working and txtfromdate and txttodate are in stringformat.
Systems Development / Support Specialist
Commented:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function validate() 
        {
            var fromDate = document.getElementById("txtStartdate").value;
            var toDate = document.getElementById("txtEnddate").value;
            if (fromDate == '' || toDate == '') 
                {
                   alert("Enter both start and end dates to click search");
                }
            else 
                {
                    if (Date.parse(fromDate) > Date.parse(toDate)) 
                    {
                        alert("Invalid Date Range!\nStart Date cannot be after End Date!")
                        return false;
                    }
                }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClientClick="validate();"
            Text="Button" />
        <asp:TextBox ID="txtStartdate" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtEnddate" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Open in new window


Try this,definitely helps
f( (new Date(t1).getTime() >= new Date(t2).getTime()))
{
    alert('t2 should be greater then t1');
    return false;
}

OR

You can also use jquery validator and your own custom validator to it.

jQuery.validator.addMethod("greaterThan",
function(value, element, params) {

    if (!/Invalid|NaN/.test(new Date(value))) {
        return new Date(value) > new Date($(params).val());
    }

    return isNaN(value) && isNaN($(params).val())
        || (parseFloat(value) > parseFloat($(params).val()));
},'Must be greater than {0}.');


Use:

$("form").validate({
    rules: {
        EndDate: { greaterThan: "#StartDate" }
    }
});

For more info:
http://stackoverflow.com/questions/833997/end-date-greater-than-start-date-jquery-validation

Author

Commented:
ok