View Full Version : How to concatenate form fields?

08-09-2006, 10:52 PM
I have the following inputs in an HTML form:

<td>(<input class="phonetext" maxlength="3" name="phoneareacode" size="3" type="text" value="">)
<input class="phonetext" maxlength="3" name="phoneexchange" size="3" type="text" value="">-
<input class="phonetext" maxlength="4" name="phonenumber" size="4" type="text" value=""></td>

I would like to combine all three of these values into one hidden field like this:

<input type="hidden" id="phone" name="phone" value="?">

With the? being the three values merged together...


08-10-2006, 12:57 AM
You can do it with JS

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function combPhone()
var areacode = document.forms[0].phoneareacode.value;
var exchange = document.forms[0].phoneexchange.value;
var phonenum = document.forms[0].phonenumber.value;
if(areacode.length = 3 && exchange.length == 3 && phonenum.length == 4)
document.forms[0].phone.value = areacode + exchange + phonenum;

<form action="#" method="post">
(<input class="phonetext" maxlength="3" name="phoneareacode" size="3" type="text">)
<input class="phonetext" maxlength="3" name="phoneexchange" size="3" type="text">-
<input class="phonetext" maxlength="4" name="phonenumber" size="4" type="text" onkeyup="combPhone()">
<input type="hidden" id="phone" name="phone">
though its better to do it with a server side language on the server end because JS can be disabled.