Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 1st, 2005, 6:54 AM   #1
crmpicco
Newbie
 
Join Date: Feb 2005
Posts: 13
Rep Power: 0 crmpicco is on a distinguished road
Question Concatinating values from drop-down into textbox

I have a problem with this code.

I am trying to perform validation on the three drop down menus which give the DATE, MONTH and the YEAR.

I am putting the values of each into a hidden text box then
performing validation on the text box.

However, i need to allow the user to change the drop down value (by pulling it down and
selecting a value) and change the textbox automatically.

It works if i select 1 date, 1 month then 1 year. But, if i change the year, month or date again it just adds
to the textbox.

for example,

works: 6/6/2005

then if i change the year again (without a refresh of the page) the box reads:

6/6/20052004

How can i stop this?

Help appreciated.

// Check the INFANT Birthday
// 2 Year Old Check

function checkBirthday(obj)
 {
	if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(obj.value) ) {
		alert( "Invalid date supplied - must be format DD/MM/YYYY" );
		//obj.focus();
		return;
	}
	
	var d = new Date();
	var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
	
	var diff = d.getDiff(d2, "y")
	if( isNaN(diff) ) {
		alert( "Invalid date supplied" );
	}
	else if( diff < 2 ) {
		alert( "This IS an INFANT \n they are younger that 2" );
		infant = true;
		document.form.submit();
		
	}
	else if( diff > 2 ) {
		alert( "This is NOT an INFANT \n they are older that 2" );
		infant = false;
		document.form.birthday.value = "";
		document.form.date.focus();
	}
	
}


<form name="form" action="abc.htm">
INF:<select name="date"
onChange="document.form.birthday.value=document.form.birthday.value+document.form.date.options[document.form.date.selectedIndex].value;"
style="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">
<%
	Response.write "<option> --- </option>"
For d = 01 to 31
	Response.write "<option value=" & d & "/>" & d & "</option>"
Next
%>
</select>
<select name="month"
onChange="document.form.birthday.value=document.form.birthday.value+document.form.month.options[document.form.month.selectedIndex].value;"
style="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">
<%
	Response.write "<option> --- </option>"
For m = 01 to 12
	response.Write "<option value=" & m & "/>" & MMtoMMM(m) & "</option>"
Next
%>		
</select>
<% 
Dim three_years_ago
three_years_ago = year(date) - 3
%>
<select name="year"
onChange="document.form.birthday.value=document.form.birthday.value+document.form.year.options[document.form.year.selectedIndex].value;"
style="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">
<%
	Response.write "<option> --- </option>"
For y = three_years_ago to year(date)
	Response.Write "<option value=" & y & ">" & y & "</option>"
Next
%>
</select>

<input type="text" name="birthday" size="5" style="font:7pt;" value="" maxLength="10"> 

<img src="images/required.gif"  onclick="javascript: checkBirthday(document.form.birthday);" value="Confirm">
</form>
crmpicco is offline   Reply With Quote
Old Jul 1st, 2005, 7:18 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Your data is being treated as text. If you add "AB" and "Z" you get "ABZ". If you add "12" and "7" you get "127". Use parseInt () to convert numerical text to a numeric type before adding. (or parseFloat, whatever's appropriate).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC