...

View Full Version : Concatinating values into a textbox



crmpicco
07-01-2005, 12:51 PM
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
07-01-2005, 03:21 PM
thats my asp function MMtoMMM



function MMtoMMM(Fdate)
Select Case Fdate
Case "1"
validdate = "JAN"
Case "2"
validdate = "FEB"
Case "3"
validdate = "MAR"
Case "4"
validdate = "APR"
Case "5"
validdate = "MAY"
Case "6"
validdate = "JUN"
Case "7"
validdate = "JUL"
Case "8"
validdate = "AUG"
Case "9"
validdate = "SEP"
Case "10"
validdate = "OCT"
Case "11"
validdate = "NOV"
Case "12"
validdate = "DEC"
end select
MMtoMMM = validdate
end function

crmpicco
07-01-2005, 03:26 PM
has anyone got code to stop this?

Harry Armadillo
07-01-2005, 03:47 PM
You'll get more responses if you just give the HTML and Javascript -- I read your first post a couple hours ago, encountered the asp, and clicked the back button.

crmpicco
07-04-2005, 12:31 PM
sorry, i was just trying to give more info

crmpicco
07-04-2005, 12:52 PM
anyone got a work around for this?

martin_narg
07-04-2005, 01:08 PM
http://www.codingforums.com/showthread.php?t=62714
http://www.codingforums.com/showthread.php?t=62705
http://www.codingforums.com/showthread.php?t=62710
http://www.codingforums.com/showthread.php?t=62558
http://www.codingforums.com/showthread.php?t=62546
http://www.codingforums.com/showthread.php?t=62560
http://www.codingforums.com/showthread.php?t=62469

crmpicco
07-04-2005, 03:11 PM
?????

asdruble
07-04-2005, 06:41 PM
The function you want is the FIRST ONE "select_data", the other two i just use to ease up building the selects (which i use a lot)...

if explanation needed, asdruble@hotmail.com - MSN

function select_data(num,dia,mes,ano,tipo){
day=eval('document.all.'+dia);
mon=eval('document.all.'+mes);
year=eval('document.all.'+ano);
datebc=eval('document.all.'+tipo);

switch (num){
case 1:
if (mon.value !=""){
if (mon.value==2) select_data(3,dia,mes,ano,tipo);
else select_data(2,dia,mes,ano,tipo);
}
break;
case 2:
if (mon.value == 2 && year.value != "" && day.value > 29) select_data(3,dia,mes,ano,tipo);
if((mon.value == 4 || mon.value == 6 || mon.value == 9 || mon.value == 11) && day.value == 31){
day.value=30;
}
break;
case 3:
if((day.value > 28 && mon.value == 2)){
if(!((+(year.value)) % 400 == 0 || (+(year.value)) % 100 != 0 && (+(year.value)) % 4 == 0)){
day.value=28;
}else{
day.value=29;
}
}
default: break;
}
datebc.value = day.value+"/"+mon.value+"/"+year.value;
}

function data_escreve(act,marcado,inicio,fim){

switch(act){
case 1:
for(i=1;i<32;i++){
if (i==marcado)
document.write('<option value="'+i+'" SELECTED>'+i+'</option>');
else
document.write('<option value="'+i+'">'+i+'</option>');
}
break;
case 2:
for(i=1;i<13;i++){
if (i==marcado)
document.write('<option value="'+i+'" SELECTED>'+i+'</option>');
else
document.write('<option value="'+i+'">'+i+'</option>');
}
break;
case 3:
for(i=inicio;i>fim;i--){
if (i==marcado)
document.write('<option value="'+i+'" SELECTED>'+i+'</option>');
else
document.write('<option value="'+i+'">'+i+'</option>');
}
break;
}
}

//ESCREVE OS SELECTS PARA SELECIONAR DATA
/*
nome = nome do campo escondido (recebido pelo script)
dia = nome do campo de DIA (unique)
mes = nome do campo de MES (unique)
ano = nome do campo de ANO (unique)
sdia = preselecao do campo DIA
smes = preselecao do campo MES
sano = preselecao do campo ANO
ano_atual = recebe data atual verdadeira do script
valor = valor inicial do campo escondido ('SDIA/SMES/SANO')
*/
function data_campos(nome,dia,mes,ano,sdia,smes,sano,ano_atual){

var valor = (sdia+'/'+smes+'/'+sano);
//HIDDEN
document.write('<input type="hidden" name="'+nome+'" value="'+valor+'">');
//DIA
document.write('<select name="'+dia+'" class="select" onChange="select_data(1,\''+dia+'\',\''+mes+'\',\''+ano+'\',\''+nome+'\')">');
document.write('<option value=""> Dia </option>');
data_escreve(1,sdia);
document.write('</select>&nbsp;');
//MES
document.write('<select name="'+mes+'" class="select" onChange="select_data(2,\''+dia+'\',\''+mes+'\',\''+ano+'\',\''+nome+'\')">');
document.write('<option value=""> Mes </option>');
data_escreve(2,smes);
document.write('</select>&nbsp;');
//ANO
document.write('<select name="'+ano+'" class="select" onChange="select_data(3,\''+dia+'\',\''+mes+'\',\''+ano+'\',\''+nome+'\')">');
document.write('<option value=""> Ano </option>');
data_escreve(3,sano,ano_atual,ano_atual-106);
document.write('</select>&nbsp;');
//INICIA VALOR
select_data(0,dia,mes,ano,nome)
}

crmpicco
07-06-2005, 11:20 AM
forename = document.form.cfname[Math.floor(i+1)].value


is this not valid JS? i is my control loop

jbot
07-06-2005, 11:25 AM
re your original question, surely you just want to clear the hidden field before adding the new values. no?!

crmpicco
07-06-2005, 11:48 AM
no i need to reference the field as clname1 for eg.

crmpicco
07-06-2005, 12:26 PM
do i not use '[' char?

crmpicco
07-06-2005, 12:46 PM
this is my javascript loop:



for (i=0; i<document.form.nochd.value-1; i++)
{
if( !/^(\d{1,2})(\d{1,2})(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DDMMYYYY" );
return;
}

var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
nochd = document.form.nochd.value
noinf = document.form.noinf.value

var diff = d.getDiff(d2, "y")

if( isNaN(diff) ) {
alert( "Invalid date supplied" );
}
else if( diff < 12 ) {
alert( "This IS an CHILD \n they are younger than 12" );
child = true;
submit_form=true;
under12counter = under12counter + 1;
passtocheckRetDate = obj.value;
checkRetDate(document.form.retdt);

}
else if( diff > 12 ) {
alert( "This is NOT an CHILD\nthey are older than 12" );
child = false;

submit_form=false;
chdcounter = chdcounter + 1; // amount of declared children that are found to be under 12
childno = i+1


number = parseFloat(i+1);


forename = document.form.cfname1.value
surname = document.form.clname1.value

if(chdcounter==2)
{
forename2 = document.form.cfname2.value
surname2 = document.form.clname2.value
}

if(chdcounter==3)
{
forename3 = document.form.cfname3.value
surname3 = document.form.clname3.value
}

}
}


I need to reference the correct form field by using the (control loop + 1). How can i build this
into my JS?

I have tried


forename = document.form.cfname[i+1].value
surname = document.form.clname[i+1].value

but it doesnt work. any ideas?

martin_narg
07-06-2005, 02:07 PM
You can use this to set your variables within a lopp, it would produce variables of forename0, forename1, forename2, etc. If your form elements start cfname1 then start the loop from 1 rather than 0.



// change myFunctionName to whatever your function name is
for( var i=0; document.form.elements["cfname"+i]; i++ ) {
myFunctionName["forename"+i] = document.form.elements["cfname"+i].value;
}


what are you doing with this information though? You already have the forenames nicely organised and easily referenced - why duplicate into variables?

m_n

crmpicco
07-06-2005, 02:15 PM
thanks, did you see http://www.codingforums.com/showthread.php?t=62785 Martin

martin_narg
07-06-2005, 02:33 PM
i'm struggling to keep track of all these posts :eek:

crmpicco
07-07-2005, 09:02 AM
I have coded this into my script, but it does not seem to work. I tried your code, but it
didnt like that either.
The value of number is correct, it is just how i am adding it into
the form name that seems to be upsetting it.
There will be multiple fields if there are more than one children. The actual name of the field is cfname1, cfname2 etc...



for (i=0; i<nochdvalue; i++) // loop through the number of children
{
number=number+1;

...code....

forename = document.form.cfname[number].value
surname = document.form.clname[number].value

}

martin_narg
07-07-2005, 09:22 AM
The code I posted is correct, it's the implementation of it that is incorrect ;)

If you could post up the error messages that you are receiving, it would be much easier to see what is going wrong, better still would be the output of your asp page.

m_n



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum