PDA

View Full Version : Need help with a form


teamdad
05-25-2003, 06:29 PM
My form code below works great if something is entered into all the fields A, B & C but I need some help with a code ...maybe an if then or something that will help with the format of the output. If something is entered in A & C only and leaving B blank, it still takes the space for it in the output of D. If either of the fields is left blank I would like to have it not be added to the output or skipped entirely so that only what has been entered will show up in the output.

Any help on this is more than greatly appreciated!!!


<html>
<SCRIPT language = JavaScript>
function translate() {
A = document.form.a.value
B = document.form.b.value
C = document.form.c.value
D = (A + ","+B+","+C)
document.form.d.value = D }
</SCRIPT>

<FORM NAME = form>
<INPUT TYPE = Text NAME = a SIZE = 20 value =""><br>
<INPUT TYPE = Text NAME = b SIZE = 20 value =""><br>
<INPUT TYPE = Text NAME = c SIZE = 20 value =""><br>
<Input Type = Button VALUE = "Translate" onClick = "translate()"><br>
<INPUT TYPE = Text NAME = d SIZE = 20 value ="">
</form>
</html>

joh6nn
05-25-2003, 10:45 PM
<SCRIPT language = JavaScript>
function translate() {
var A, B, C, df = document.form;
A = (df.a.value != '') ? df.a.value + ', ' : df.a.value
B = (df.b.value != '') ? df.b.value + ', ' : df.b.value
C = df.c.value
df.d.value = (A + B + C); }
</SCRIPT>

(something) ? this : that ;

that's a different way of saying

if (somethign) { this }
else { that }

so, in this case,
if (df.a.value != '') { A = df.a.value + ', ';}
else { A = df.a.value; }

got it?

teamdad
05-25-2003, 11:10 PM
I have tried and tried to rack my brain out with everything else that failed every time...... thanks so much for the help.

Teamdad & son.