Solved

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

Posted on 2004-09-06
9
904 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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