Form Field Validation

  • Thread starter Thread starter Steve
  • Start date Start date
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.
 
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.
 
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
 
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
 
Back
Top