Go Back   kirupaForum > Development > Client-Side (HTML, CSS, JavaScript)

Reply
 
Thread Tools Display Modes
Old 11-28-2009, 06:28 PM   #1
thevibe
Registered User
Issue with a form data html

Hi guys,

I am working on a form and I encountered a problem. I started with a basic form that is attach to a server, so the rules are fixed. The data names can't be change. The form is almost working except for 1 part "date of birth".

This is the code from the basic form for the date of birth;

<input type="text" name="txtContactBDay" size=34>

The problem with this is that if someone is writing 12 january 2004 it won't work, but the user won't know it. The data sent really needs to be in this specific format... mm/dd/yyyy (eg. 10/25/1982 (the forward-slash are really important in the data too)).

So I changed the script to have some option boxes for the year the month and the day. I am wondering how can I write the script that at the end the data gathered is in the good format mm/dd/yyyy and is named "txtContactBDay".

Here's an example of how I scripted the option boxes;

<select name="month" id="date" onchange="return wait_for_load(this, event, function() { editor_date_month_change(this, 'birthday_day','birthday_year'); });">
<option value="na">Month</option>
<option value="1">January</option>...

<select name="day">
<option value="na">Day</option>
<option value="1">1</option>...

<select name="year">
<option value="na">Year</option>
<option value="2009">2009</option>...


Thanks guys !
thevibe is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-29-2009, 06:44 AM   #2
icio
looks better in lowercase
 
icio's Avatar
Location Edinburgh, Scotland

Posts 3,689
You realise that if the user doesn't have JavaScript turned on then your form just fails? You should fix the server script.

If you're sticking to your dropdowns then I recommend adding a hidden txtContactBday with a value based on the dropdowns just before the form is submitted.

To do this, add an onsubmit event to the form element that does something like this:
Code:
<html>
    <head>
        <script type="text/javascript">
        
            function validate()
            {
                var ok = true;
                
                // Check each has a value selected
                ok = ok && document.getElementById("bdayDay").selectedIndex > 0;
                ok = ok && document.getElementById("bdayYear").selectedIndex > 0;
                ok = ok && document.getElementById("bdayMonth").selectedIndex > 0;
                
                // Should probably also check that the value corresponds to a valid date
                // (e.g. not 30th February)
                
                if (!ok)
                {
                    alert("Please select your birthday.");
                    return false;
                }
                
                return true;
            }
        
            function consolidateBirthday(form)
            {
                // Get the elements
                var day = document.getElementById("bdayDay");
                var year = document.getElementById("bdayYear");
                var month = document.getElementById("bdayMonth");
                
                // Get their values
                day = day.options[day.selectedIndex].value;
                year = year.options[year.selectedIndex].value;
                month = month.options[month.selectedIndex].value;
                
                // Construct the date value
                var date = month + '/' + day + '/' + year;
                
                // Create and add the input
                var input = document.createElement("input");
                input.type = "hidden";
                input.name = "txtContactBDay";
                input.value = date;
                
                form.appendChild(input);
                return true;
            }
            
        </script>
    </head>

    <body>
        <noscript>You need to turn on javascript!</noscript>
        <form onsubmit="return validate() &amp;&amp; consolidateBirthday(this)">
            <strong>Birthday:</strong>
                <select id="bdayMonth">
                    <option>Month</option>
                    <option value="1">Jan</option>
                    <option value="2">Feb</option>
                    <option value="3">Mar</option>
                    <option value="4">Apr</option>
                    <option value="5">May</option>
                    <option value="6">Jun</option>
                </select>
                <select id="bdayDay">
                    <option>Day</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                </select>
                <select id="bdayYear">
                    <option>Year</option>
                    <option>1989</option>
                    <option>1990</option>
                    <option>1991</option>
                    <option>1992</option>
                    <option>1993</option>
                    <option>1994</option>
                    <option>1995</option>
                </select>
                <input type="submit" />
        </form>
    </body>
    
</html>
Hope that helps
icio is offline   Reply With Quote
Old 11-29-2009, 08:51 PM   #3
icio
looks better in lowercase
 
icio's Avatar
Location Edinburgh, Scotland

Posts 3,689
In thevibe's form is trying to include this onsubmit action in the following manner:
HTML Code:
<form ... onSubmit="return ValidateRequiredFields();","return validate() &amp;&amp; consolidateBirthday(this)" />
Note that this is invalid HTML, both in that `attribute="",""` makes no sense and `onSubmit` should be the all lowercase `onsubmit`. The form tag should instead resemble:
HTML Code:
<form ... onsubmit="return ValidateRequiredFields() &amp;&amp; validate () &amp;&amp; consolidateBirthday(this);" />
icio is offline   Reply With Quote
Old 11-29-2009, 09:02 PM   #4
thevibe
Registered User
Hi icio, it still doesn't work, but thanks anyway and good luck for your exams !
thevibe is offline   Reply With Quote
Old 11-29-2009, 09:09 PM   #5
icio
looks better in lowercase
 
icio's Avatar
Location Edinburgh, Scotland

Posts 3,689
The script works fine. Notice this line in my JavaScript script:
Code:
var date = month + '/' + day + '/' + year;
This puts the slashes between the components of the date. So take the slashes out of the your value attributes in the options for the date dropdowns. They are redundant.
icio is offline   Reply With Quote
Old 11-29-2009, 09:26 PM   #6
thevibe
Registered User
THANKS ICIO, you did it again. Everything is working now. You have been really helpful ! Sorry again for the trouble
thevibe is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:20 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com