Form Field Validation

S

Steve

HI All...been a while since I was last here.

FP2002

I am trying to use some javascript to validate fields in a form based on an
option being selected. (I don't think I can do this using the FP validation
stuff.)

The validation is working for the options fields, so I will just show the
conditional code I am trying to develop:

// check to see if the field is blank
if (theForm.paymentOption.value == "V1")
{
if (theForm.test1.value == "")
{
alert("You must enter an alias.");
theForm.test1.focus();
return (false);
}
}

This is not working, and I can't seem to find the cause. If anyone has any
ideas, I would should appreciate the help.

Steve G.
 
S

Steve

Update:
If I hard code the optional value from the form, as shown below, then the
script works as I hoped, so I guess the problem is getting the value from
the form:
var opVal = "V1";
if (opVal == "V1")
{
if (theForm.test1.value == "")
{
alert("You must enter an alias.");
theForm.test1.focus();
return (false);
}
}

opVal is the hard coded valued of an radio button option group.

Steve G

HI All...been a while since I was last here.

FP2002

I am trying to use some javascript to validate fields in a form based on an
option being selected. (I don't think I can do this using the FP validation
stuff.)

The validation is working for the options fields, so I will just show the
conditional code I am trying to develop:

// check to see if the field is blank
if (theForm.paymentOption.value == "V1")
{
if (theForm.test1.value == "")
{
alert("You must enter an alias.");
theForm.test1.focus();
return (false);
}
}

This is not working, and I can't seem to find the cause. If anyone has any
ideas, I would should appreciate the help.

Steve G.
 
J

Jon Spivey

Steve,
You can't grab the value of a radio button directly, you'd need to address
each radio in turn and see if it's checked.
<script type="text/javascript">
function whichOne(f){
for(i=0;i<f.length;i++){
if(f.checked){
alert('Value was ' + f.value);
break;
}}}
</script>
<form onsubmit="whichOne(this.Country)">
Where do you live?
UK<input type="radio" value="UK" checked name="Country">
USA<input type="radio" value="USA" name="Country">
Elsewhere<input type="radio" value="Elsewhere" name="Country">
<input type="submit" value="Submit">
</form>

Jon
 
S

Steve

Thanks Jon...as always, you were a very big help.

Steve G

Steve,
You can't grab the value of a radio button directly, you'd need to address
each radio in turn and see if it's checked.
<script type="text/javascript">
function whichOne(f){
for(i=0;i<f.length;i++){
if(f.checked){
alert('Value was ' + f.value);
break;
}}}
</script>
<form onsubmit="whichOne(this.Country)">
Where do you live?
UK<input type="radio" value="UK" checked name="Country">
USA<input type="radio" value="USA" name="Country">
Elsewhere<input type="radio" value="Elsewhere" name="Country">
<input type="submit" value="Submit">
</form>

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top