 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
wkilc
Joined: 20 Mar 2005 Posts: 6
|
Posted: Tue Feb 19, 2008 6:14 pm javascript form validation |
|
|
|
Hi all,
I'm using a JS form validation script from Dynamic Drive. A while back, the script's author customized it for me to allow for some very specific "dependency checking".
All I'm trying to do now is change one form element from a "pulldown" menu to a few radio buttons.
If field 1 (Vehicle) is answered "Toyota", then the second field (Model) becomes required (cannot be left blank). Works fine if "Model" is a text field or a pull-down... I cannot make it work with radio buttons or check boxes.
| Code: |
function formCheckToyota(formobj)
{
for (var i = 1; i <= 3; i++ )
{
if ( formobj.elements['Vehicle'+i].value == 'Toyota' )
{
if ( formobj.elements['Model'].value == '')
{
alert('Error: please select a vehicle model');
return false;
}
}
}
return true;
} |
This was one of my best attempts...
| Code: |
function formCheckToyota(formobj)
{
for (var i = 1; i <= 3; i++ )
{
if ( formobj.elements['Vehicle'+i].value == 'Toyota' )
{
if ( formobj.elements['Model'].[0]checked )
{
alert('Error: please select a vehicle model');
return false;
}
}
}
return true;
} |
...no dice. The base version of the script CAN check radio boxes, but I don't know enough to discern a solution. Here's the basic checking code from the script, if it helps:
| Code: |
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
if (obj.type == null){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " " + fieldDescription[i] + "\n";
}
continue;
}
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " " + fieldDescription[i] + "\n";
}
break;
default: |
Thanks for reading,
~Wayne |
|
kanenas

Joined: 14 Dec 2004 Posts: 191
|
Posted: Sun Mar 16, 2008 9:34 am Re: javascript form validation |
|
|
|
| wkilc wrote: |
[...]
| Code: |
function formCheckToyota(formobj)
{
for (var i = 1; i <= 3; i++ )
{
if ( formobj.elements['Vehicle'+i].value == 'Toyota' )
{
if ( formobj.elements['Model'].[0]checked ) |
|
You've transposed the '.' and '[]' operators. It isn't clear to me from the sample, but you also probably need to loop over 'formobj.Model', looking for checked checkboxes beyond the first. |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|
 |
|
|
|
|
|
|