Solved

Validation for age using asp.net, How??????????????????

Posted on 2004-09-06
9
887 Views
Last Modified: 2012-06-22
I need some validation for my form, i have a text box where you enter your age, I need some sort some validation which will not allow someone under 18 year of age!

Can someone help, thanks.

PS i am using asp.net vbscript.
0
Comment
Question by:pigmentarts
9 Comments
 
LVL 20

Expert Comment

by:ihenry
Comment Utility
This RangeValidator control validates when TextBox1 has integer value range from 18 to 100

<asp:RangeValidator id="RangeValidator1" runat="server" ErrorMessage="RangeValidator" Display="Dynamic"
      ControlToValidate="TextBox1" Type="Integer" MinimumValue="18" MaximumValue="100"></asp:RangeValidator>
0
 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
sorry i ment to say there enter there birth date no there age, so i need something that checks this date to see if they are over 18 years old.
0
 
LVL 8

Expert Comment

by:razo
Comment Utility
well u can use a custom validator or u can use a javascript function to do the validation
0
 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
i need a  custom validator, any example in vb for me?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
did it this way

<asp:CustomValidator ClientValidationFunction='"dob" > DateTime.Now.AddYears(-18)' ControlToValidate="dob" Display="Dynamic" EnableClientScript="true" ErrorMessage="you must be over 18" ID="dobValidator" runat="server" />


seems to work fine.
0
 
LVL 8

Assisted Solution

by:shovavnik
shovavnik earned 250 total points
Comment Utility
The best thing is to use a few validators for this.

1. A RequiredValidator.  This ensure the user enters something in the field.

2. A CompareValidator, with the following properties set:
- Operator = DataTypeCheck
- ControlToCompare is empty (no value)
- Type = Date
This ensures the user enters a valid date to begin with

3. Another CompareValidator (I'll call it here "AgeCompareValidator", with the following properties set:
- Operator = LessThanEqual
- Type = Date
On the server-side, in Page_Load, add the following lines:

DateTime MaxBirthDate = new DateTime();
// set the proper date of MaxBirthDate.  The proper date is today - 18 years.
AgeCompareValidator.ValueToCompare = MaxBirthDate.ToString();
0
 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
i can not get the script to work i can not convert it.

Exception Details: System.Web.HttpException: The value '01/01/0001 00:00:00' of the ValueToCompare property of 'AgeCompareValidator' cannot be converted to type 'Date'.
0
 
LVL 12

Author Comment

by:pigmentarts
Comment Utility
shovavnik
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 250 total points
Comment Utility
Public Function GetAge(ByVal Birthdate As System.DateTime, _
    Optional ByVal AsOf As System.DateTime = #1/1/1700#) _
    As String

        'Don't set second parameter if you want Age as of today

        'Demo 1: get age of person born 2/11/1954
        'Dim objDate As New System.DateTime(1954, 2, 11)
        'Debug.WriteLine(GetAge(objDate))

        'Demo 1: get same person's age 10 years from now
        'Dim objDate As New System.DateTime(1954, 2, 11)
        'Dim objdate2 As System.DateTime
        'objdate2 = Now.AddYears(10)
        'Debug.WriteLine(GetAge(objDate, objdate2))

        Dim iMonths As Integer
        Dim iYears As Integer
        Dim dYears As Decimal
        Dim lDayOfBirth As Long
        Dim lAsOf As Long
        Dim iBirthMonth As Integer
        Dim iAsOFMonth As Integer

        If AsOf = "#1/1/1700#" Then
            AsOf = DateTime.Now
        End If
        lDayOfBirth = DatePart(DateInterval.Day, Birthdate)
        lAsOf = DatePart(DateInterval.Day, AsOf)

        iBirthMonth = DatePart(DateInterval.Month, Birthdate)
        iAsOFMonth = DatePart(DateInterval.Month, AsOf)

        iMonths = DateDiff(DateInterval.Month, Birthdate, AsOf)

        dYears = iMonths / 12

        iYears = Math.Floor(dYears)

        If iBirthMonth = iAsOFMonth Then
            If lAsOf < lDayOfBirth Then
                iYears = iYears - 1
            End If
        End If
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now