Click here to Skip to main content
14,699,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends
i have to text box fist is birth date and second is join date and my problem is how to validate difference between 20 year birth date and join date pls help me

and it is possible to validate to validation control
Posted
Comments
Prasad Khandekar 21-May-13 7:14am
   
Hello Rashmikant,

See this (http://stackoverflow.com/questions/2309912/asp-net-validator-to-compare-two-date-difference-is-not-more-than-12-months) link.

HI Rashmikants Patel,

You can use asp Compare Validator for this purpose.

http://www.w3schools.com/aspnet/control_comparevalidator.asp[^]

OR

http://msdn.microsoft.com/en-us/library/db330ayw(v=vs.71).aspx[^]

Hope It will help..
   
Comments
[no name] 21-May-13 7:17am
   
pls give me example in asp.net
well dude.. for example if you have two textboxes to enter "Date of Birth" & "Date of Joining" and you
want the user to enter the date of birth which should have to be the earlier than Date of Joining.
I.e. user was born on 12 feb 1989 and he joined your office etc on 12th may 2013... so you can use
compare validators to do this.

see the code below which might help you to clarify what i meant to say.



<asp:comparevalidator id="Validator1" runat="server" Display = "None" ControlToCompare="txtDateofBirth" ControlToValidate="txtDateofJoining" ErrorMessage="Date of Birth must be before Date of Joining." Operator="GreaterThan" Type="Date" ></asp:comparevalidator> 


in the above line of code validator will check the value of joining date with DOB and will show the error message
if user enters the date of birth as of 2013 & date of joining as of 1989.

Regards.
   
v2
Comments
VICK 21-May-13 7:55am
   
does it gives you a better idea/example??? :)
You can have your markup as something like this

<asp:textbox id="startDate" runat="server" xmlns:asp="#unknown"></asp:textbox>
       <asp:textbox id="endDate" runat="server" xmlns:asp="#unknown"></asp:textbox>
       <asp:customvalidator id="cv" runat="server" validationgroup="vg" clientvalidationfunction="validateYears" errormessage="invalid date" display="None" xmlns:asp="#unknown"></asp:customvalidator>
       <asp:button id="btn" runat="server" text="Click" validationgroup="vg" xmlns:asp="#unknown" />
       <asp:validationsummary id="vs" runat="server" validationgroup="vg" showmessagebox="true" showsummary="false" xmlns:asp="#unknown" />


and add a js validation method

function validateYears(source, args) {
            startYear = new Date(document.getElementById("startDate").value);
            endYear = new Date(document.getElementById("endDate").value);
            alert(endYear.getFullYear() - startYear.getFullYear() >= 20);
            args.IsValid = endYear.getFullYear() - startYear.getFullYear() >= 20;            
        }


the function here gets the difference in years and validates for a difference of 20 years, you might also want to consider difference in months or days.

However, i would not recommed using text boxes for dates as users may use multiple ways to enter a date
   
Comments
VICK 21-May-13 8:43am
   
"" However, i would not recommed using text boxes for dates as users may use multiple ways to enter a date ""

Samresh is right... but to overcome this we can use MaskExtender and Calendar Control... :)
[no name] 21-May-13 10:32am
   
thanks dude

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900