Solved

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

Posted on 2004-09-06
9
894 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
 
LVL 12

Author Comment

by:pigmentarts
ID: 11988586
i need a  custom validator, any example in vb for me?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

13 Experts available now in Live!

Get 1:1 Help Now