View Full Version : Coding problem with <input type=text

04-09-2004, 04:25 PM
I have had a problem reported by users over a period of three years. I'm sure everyone that sees it doesn't report it, but with 8,000 visitors a day it only gets reported about once every three months so it is very obscure. Computers/program errors are usually consistent so this confuses me.

I have an <input box where users enter M or F to indicate their sex. If they don't do it correctly, they get an alert message. The problem is that they swear they have entered M or F but still get the message.

Now today for the first time a user tells me the <input box contains: onChange='parent.create.setSex(this)'

Here is all the relevant code:

function setSex(a){Sx=a.value;
if (Sx!="m" && Sx!="M" && Sx!="f" && Sx!="F"){alert("Please enter M or F to indicate your Sex.")}
if (Sx=="m"){Sx="M"; parent.image.document.f1.sx.value="M"}
if (Sx=="f"){Sx="F"; parent.image.document.f1.sx.value="F"}

// Sex box
c+="<tr><td align=center class='formfields'>";
c+="<input type=text name=sx value="+Sx+" onChange='parent.create.setSex(this)' ";
c+="size=1 maxlength=1 class='formfields' border=1>";

// Age box
c+="<input type=text name=ag value="+Ag+" onChange='parent.create.setAg(this)' ";
c+="size=3 maxlength=4 class='formfields' border=1>";

Note that the age box is coded similarly but it has never caused a problem.

Up until a week ago I used & where you now see &&. I posted here and learned the difference but evidently it wasn't the problem.

Thanks for any ideas, Peter

04-09-2004, 07:03 PM
Could you post the URL to this form?

Simplify it a little? Assume they enter the correct data and process that first. If Sx=='m' AND Sx=='f' both fail, then it alerts, not before.

if(Sx == "m") {Sx="M"; parent.image.document.f1.sx.value="M"}
else if (Sx == "f") {Sx="F"; parent.image.document.f1.sx.value="F"}
else {alert("Please enter M or F to indicate your Sex.")}

04-09-2004, 07:09 PM
Or, since the majority of the time the answer is going to be one or the other, use a radio group?

M <input type="radio" name="sex" value="m">
F <input type="radio" name="sex" value="f">

function setSex(r){
if(r[0].checked) parent.image.document.f1.sx.value=r[0].value;
else if(r[1].checked) parent.image.document.f1.sx.value=r[1].value;
else alert("Please check M or F to indicate your Sex.");

04-09-2004, 10:21 PM
Thanks for the replies.

First, your simplified code does look better. I'm self-taught at this and it doesn't come easy.

Second, the radio buttons really do make sense. I think when I coded it a few years ago I just wanted to use text boxes for all input for consistency. Actually, I think I'll just make it a drop down menu.

That will certainly eliminate the problem, though I will remain curious how it could happen so irregularly.

FYI my webpage is http://www.caloriesperhour.com/index_burn.html