Web Design and Development Forums

[SOLVED] Validation code problem

This is a discussion on "[SOLVED] Validation code problem" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Validation code problem are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > JavaScript Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Apr 23rd, 2008, 17:10   #1 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: England
Age: 20
Posts: 21
Question [SOLVED] Validation code problem

I have a piece of code that validates whether one button has been selected from a bunch of dynamically created radio buttons.

Code: Select all
//Validation Code
   var checked = false; 
   var Sizes = document.BuyProduct.Sizes;
   for (var i=0; i<Sizes.length; i++)  
   {  
     if (Sizes[i].checked) {  
       checked = true;   
     }  
   }
    
   if(!checked) 
   {  
      Errors++
      document.getElementById('mySpan1a').style.display='block';
      document.getElementById('mySpan1a').innerHTML='Please Select a Size!';
      document.getElementById('mySpan1b').style.display='inline';
      document.getElementById('mySpan1b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan1a').style.display='none';
      document.getElementById('mySpan1a').innerHTML='';
      document.getElementById('mySpan1b').style.display='none';
      document.getElementById('mySpan1b').innerHTML='';
   }
// Form code
<?php echo $Err1?><span class="mySpan1b" id="mySpan1b"></span><span class="head4">Available Sizes:</span><span class="errmsg"> *</span>
<table style="position:relative;"><tr>
 
<?php
$counter=0;
$X=0;
$i=0;
$IsEmpty1 == false;
while ($IsEmpty1 == false) 
  {
     if($AvailableSizes[$X] != "")
     {
     $id1 = "Size".$i;
        if($counter%6==0&&$counter!=0) echo "</tr><tr>";
        echo"<td>$AvailableSizes[$X]</td>";
     echo "<td><label><input type='radio' id='$id1' name='Sizes' value='$AvailableSizes[$X]'";
     if($AvailableSizes[$X] == $SizeSel){echo "Checked";} echo"></label></td></tr><tr>";  
        ++$counter;
     }
     else
     {
        $IsEmpty1 = true;
     }
     $i++;
     $X++;
  }
?>
</tr></table>
The validation code works, but to a point. This code assumes that there will be more than 1 radio button to choose from and becomes an array, however occassionally there will be only a single button which doesn't turn the selection into an array, and thus stops the validation code from working and displaying the error message for that option.

So what I need is a modified version of the validation that can account for when the selection is an array and when it is only a single option as well.
psycho wolvesbane is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 17:31   #2 (permalink)
Nerdy Moderator
 
CloudedVision's Avatar
 
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 520
Blog Entries: 4
Re: Validation code problem

I'm in too much of a rush to check your code, but if theres only one value, can't you still make it an array?
__________________
Take it easy

Other Road Design

WebForumz Moderator: HTML | Javascript | PHP
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 17:35   #3 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: England
Age: 20
Posts: 21
Re: Validation code problem

I'm not sure how to do that.

Also I tried obtaining the Length of Sizes from both array and single, and the array gave me a value and the single gave me an undefined answer.
psycho wolvesbane is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 20:39   #4 (permalink)
Nerdy Moderator
 
CloudedVision's Avatar
 
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 520
Blog Entries: 4
Re: Validation code problem

im confused. exactly what variable is sometimes an array and sometimes not?
__________________
Take it easy

Other Road Design

WebForumz Moderator: HTML | Javascript | PHP
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 23:23   #5 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: England
Age: 20
Posts: 21
Re: Validation code problem

The form element Sizes which is a radio button. Since it's being dynamically added depending on the mysql database entries it could have multiple radio buttons grouped under the same name, Sizes, making it an array.

However if there is only the 1 entry in the database the single radio button doesn't become an array and the validation can't do anything about it as it was written to handle arrays only.

Edit:

Anyway I got help from another form

Code: Select all
   var checked = false; 
   var Sizes = document.BuyProduct.Sizes;

   if (Sizes.length){
      for (var i=0; i<Sizes.length; i++)  
      {  
         if (Sizes[i].checked) {  
         checked = true;   
         }  
      }
   }
   else if(Sizes.checked)
   {
      checked=true;
   }
 
   if(!checked) 
   {  
      Errors++
      document.getElementById('mySpan1a').style.display='block';
      document.getElementById('mySpan1a').innerHTML='Please Select a Size!';
      document.getElementById('mySpan1b').style.display='inline';
      document.getElementById('mySpan1b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan1a').style.display='none';
      document.getElementById('mySpan1a').innerHTML='';
      document.getElementById('mySpan1b').style.display='none';
      document.getElementById('mySpan1b').innerHTML='';
   }

Last edited by psycho wolvesbane; Apr 23rd, 2008 at 23:27.
psycho wolvesbane is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 24th, 2008, 00:28   #6 (permalink)
Nerdy Moderator
 
CloudedVision's Avatar
 
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 520
Blog Entries: 4
Re: Validation code problem

Give this a shot:

Code: Select all
var checked = false; 
   var Sizes = document.BuyProduct.Sizes;

   if(typeof(Sizes[0].checked)=="undefined") {
      Sizes[0] = Sizes;
   }

   if (Sizes.length){
      for (var i=0; i<Sizes.length; i++)  
      {  
         if (Sizes[i].checked) {  
         checked = true;   
         }  
      }
   }
   else if(Sizes.checked)
   {
      checked=true;
   }
 
   if(!checked) 
   {  
      Errors++
      document.getElementById('mySpan1a').style.display='block';
      document.getElementById('mySpan1a').innerHTML='Please Select a Size!';
      document.getElementById('mySpan1b').style.display='inline';
      document.getElementById('mySpan1b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan1a').style.display='none';
      document.getElementById('mySpan1a').innerHTML='';
      document.getElementById('mySpan1b').style.display='none';
      document.getElementById('mySpan1b').innerHTML='';
   }
__________________
Take it easy

Other Road Design

WebForumz Moderator: HTML | Javascript | PHP
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 24th, 2008, 10:16   #7 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: England
Age: 20
Posts: 21
Re: Validation code problem

The code I got above works so this is topic solved.
psycho wolvesbane is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Php code problem longstand PHP Forum 2 Nov 8th, 2007 19:14
[SOLVED] image display php code problem longstand PHP Forum 7 Oct 19th, 2007 17:28
[SOLVED] Simple Php Code Problem longstand PHP Forum 3 Oct 17th, 2007 21:02
Captcha validation code tolis ASP Forum 0 Feb 14th, 2007 18:13
Validation problem, please help swiftd HTML Forum 1 Apr 6th, 2004 08:04



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 19:22.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59