Solved

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

Posted on 2004-09-06
9
897 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
ID: 11988538
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
ID: 11988553
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
ID: 11988563
well u can use a custom validator or u can use a javascript function to do the validation
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 12

Author Comment

by:pigmentarts
ID: 11988586
i need a  custom validator, any example in vb for me?
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 11988600
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
ID: 11988715
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
ID: 11989164
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
ID: 11989169
shovavnik
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 250 total points
ID: 11989569
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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