Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Need help with zip and date field validation

    I am new to ASP.Net and am trying to do a project to help me learn the basics of this coding language. Below is my code and I was wondering if anyone could help me with a couple parts of this form.

    First, I am having trouble calculating the users age from the date of birth and displaying a message saying the user needs to be over 18 years old.

    Second, I need help with making sure the user enters a valid date in the form and the reservation date should be at least 2 days ahead of the current date and the return date should be later then the reservation date.

    Any help is greatly appreciated.

    Below is the code I have:

    Code:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>XYZ Sports Rental</title>
    </head>
    
    <body>
    
        <h1>XYZ Sports Rental</h1>
        
        <%
            //assign each variable in form
            string equipment = Request.Form["equipment"];
            string pickupDate = Request.Form["pickupDate"];
            string pickupHours = Request.Form["pickupHours"];
            string returnDate = Request.Form["returnDate"];
            string returnHours = Request.Form["returnHours"];
            string firstName = Request.Form["firstName"];
            string lastName = Request.Form["lastName"];
            string street = Request.Form["street"];
            string city = Request.Form["city"];
            string zip = Request.Form["zip"];
            string date = Request.Form["date"];
    
            
            //check for empty fields
            if (Request.Form["submitted"] == "true")
            {
                if (Request.Form["equipment"] == "Select Equipment Type" ||
                    Request.Form["equipment"] == "" ||
                   Request.Form["pickupDate"] == "null" ||
                    Request.Form["pickupDate"] == "" ||
                   Request.Form["pickupHours"] == "hr" ||
                    Request.Form["pickupHours"] == "" ||
                   Request.Form["returnDate"] == "hr" ||
                    Request.Form["returnDate"] == "" ||
                   Request.Form["returnHours"] == "hr" ||
                    Request.Form["returnHours"] == "" ||
                   Request.Form["firstName"] == "null" ||
                    Request.Form["firstName"] == "" ||
                   Request.Form["lastName"] == "null" ||
                    Request.Form["lastName"] == "" ||
                   Request.Form["street"] == "null" ||
                    Request.Form["street"] == "" ||
                   Request.Form["city"] == "null" ||
                    Request.Form["city"] == "" ||
                   Request.Form["zip"] == "null" ||
                    Request.Form["zip"] == "" ||
                   Request.Form["date"] == "null" ||
                    Request.Form["date"] == "")
                    {
                        Response.Write("<p><strong><center>You must enter values in each field.</center></strong></p>");
                    }
                else
                    Response.Write("<p><strong>Your reservation has been submitted.</strong></p>");
            }
            
        %>
        
        <form id="resForm" runat="server">
            <table border = "0">
                <tr>
                    <td>
                        Equipment:<br/>
                        <select name = "equipment">
                            <option value="unselected">Select Equipment Type</option>
                            <option value = "20">Fishing Boat</option>
                            <option value = "15">Kayak</option>
                            <option value = "2">Mountain Bike</option>
                            <option value = "10">Scuba Gear</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        Pick-up Date: <br/>
                        <input type = "text" id = "dateIn" name = "pickupDate" />
                    </td>
                    <td>
                        Pick-up Time: <br/>
                        <select name = "pickupHours">
                            <option value="unselected">hr</option>
                            <option value = "7">07</option>
                            <option value = "8">08</option>
                            <option value = "9">09</option>
                            <option value = "10">10</option>
                            <option value = "11">11</option>
                            <option value = "12">12</option>
                            <option value = "13">13</option>
                            <option value = "14">14</option>
                            <option value = "15">15</option>
                            <option value = "16">16</option>
                            <option value = "17">17</option>
                        </select>
    
                    </td>
                </tr>
                <tr>
                    <td>
                        Return Date: <br/>
                        <input type = "text" id = "dateOut" name = "returnDate" />
                    </td>
                    <td>
                        Return Time: <br/>
                        <select name = "returnHours">
                            <option value="unselected">hr</option>
                            <option value = "7">07</option>
                            <option value = "8">08</option>
                            <option value = "9">09</option>
                            <option value = "10">10</option>
                            <option value = "11">11</option>
                            <option value = "12">12</option>
                            <option value = "13">13</option>
                            <option value = "14">14</option>
                            <option value = "15">15</option>
                            <option value = "16">16</option>
                            <option value = "17">17</option>
                        </select>
                       
                    </td>
                </tr>
                <tr>
                    <td>
                        First Name: <br/>
                        <input type = "text" name = "firstName"/>
                    </td>
                    <td>
                        Last Name: <br/>
                        <input type = "text" name = "lastName"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        Street: <br/>
                        <input type = "text" name = "street"/>
                    </td>
                    <td>
                        City: <br/>
                        <input type = "text" name = "city"/>
                    </td>
                    <td>
                        Zip:<br/>
                        <input type = "text" name = "zip" maxlength = "5"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        Date of Birth: <br/>
                        <input type="text" name="date"  />
                    </td>
                </tr>
                <tr>
                    <td colspan = "3" align = "center">
                        <input type = "submit" name = "submit" value = "Submit Reservation" />
                        <input type = "hidden" name = "submitted" value = "true" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hi Broll05,

    Answers to all your questions can be easily done using the System.DateTime class (http://msdn.microsoft.com/en-us/libr....datetime.aspx)

    A) To calculate the age, here's a sample link:
    http://snippets.dzone.com/posts/show/1632

    B) To validate an entered date string, here's a sample link:
    http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

    C) To compare dates, here's a sample link:
    http://stackoverflow.com/questions/5...an-30-days-old


    A few more things I immediately noticed about your code that I thought would be worth mentioning:

    1. In the beginning of codebehind, you're storing every form field request in separate variables. Then, inside your if statement, you're requesting them again. No need to do that, just refer to the variables. Plus, I'm pretty sure you're losing some efficiency by doing it this way.

    2. What's the purpose of this: if (Request.Form["submitted"] == "true")? I don't think you need this hidden control.

    3. Don't put HTML tags inside Response.Write. This is a bad practice.


    Regards,
    Mike


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •