HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
javascript form validation
Post new topic   Reply to topic    HTML Help Forum Index -> HTML Form
View previous topic :: View next topic  
Author Message
wkilc



Joined: 20 Mar 2005
Posts: 6

PostPosted: Tue Feb 19, 2008 6:14 pm     javascript form validation Reply with quote

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

PostPosted: Sun Mar 16, 2008 9:34 am     Re: javascript form validation Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> HTML Form All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
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
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial