Radio Buttons and Javascript

This is a discussion on "Radio Buttons and Javascript" within the JavaScript Forum section. This forum, and the thread "Radio Buttons and Javascript are both part of the Program Your Website category.



 Subscribe in a reader

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

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Dec 11th, 2007, 10:19
New Member
Join Date: Dec 2007
Location: Northern Ireland, UK
Age: 35
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Radio Buttons and Javascript

Hi I have the following form:
HTML: Select all
<form id="support_tools" name="support_tools" method="post" action="#">
                    <table width="100%" border="0" cellspacing="4" cellpadding="0">
                      <tr>
                        <td class="H1_Orange_no_background"><table width="100%" border="0" cellspacing="4" cellpadding="0">
                            <tr>
                              <td width="29%" height="20"><label>
                                <input name="search_type" type="radio" value="email">
                                Email</label></td>
                              <td width="35%"><label>
                                <input name="search_type" type="radio" value="mac" />
                                MacID</label></td>
                              <td width="36%"><label>
                                <input name="search_type" type="radio" value="name" />
                                Surname</label></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="H1_Orange_no_background"><input name="input" type="text" class="H1_Blue_no_background" size="45"/></td>
                      </tr>
                      <tr>
      <tr>
                        <td class="H1_Orange_no_background"> Show 
      <select name="session_type" class="H1_Blue_no_background">
      <option value="all" selected="selected">All Sessions</option>
      <option value="active">Active Sessions</option>
      <option value="complete">Complete Sessions</option>
      </select>
      </tr>
                      <tr>
                        <td><input name="Submit22" type="submit" class="H1_Blue_no_background" value="Search" /></td>
                      </tr>
                    </table>
                </form>
I want to check which radio button has been selected and then perform validation depending on the type eg. Email, Mac, Surname

I have the validation script ok, but its figuring out which radio button has been selected that is not working for me... Any ideas/help would be great.

HTML: Select all
<script type="text/javascript">
    Validator('support_tools');
     for (i=0;i<document.forms[0].search_type.length;i++) {
       if (document.forms[0].search_type[i].checked) {
        user_input = document.forms[0].search_type[i].value;
       }
      }
    var a = "email";
    var b = "name";
    if (user_input == a)
     {
     add_validation('input', 'req', 'You need to enter an Email Address');
     add_validation('input', 'email', 'Your Email seesm to be invalid');
     }
    if (user_input == b)
     {
     add_validation('input', 'req', 'You need to enter a Mac ID');
     add_validation('input', 'mac', 'Your MacID is invalid');
     }
    </script>
Regards

Nathan

Last edited by Rakuli; Dec 11th, 2007 at 10:22. Reason: Added [html][/html] tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Dec 11th, 2007, 10:26
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Radio Buttons and Javascript

So it appears you have a script there that at a quick glance does the trick. Are you having a problem with this?
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Dec 11th, 2007, 10:28
New Member
Join Date: Dec 2007
Location: Northern Ireland, UK
Age: 35
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Radio Buttons and Javascript

Hi no matter which radio button i chooses it always performs the first if statement.

Regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Dec 11th, 2007, 10:29
New Member
Join Date: Dec 2007
Location: Northern Ireland, UK
Age: 35
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Radio Buttons and Javascript

And i get a javascript error:

Line 212
Char 5
Error: 'user_input is undefined'
Code 0
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Dec 11th, 2007, 10:47
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Radio Buttons and Javascript

Try cleaning up the code a little

Code: Select all
<script type="text/javascript">

Validator('support_tools');

var pageForm = document.getElementById('support_tools');

var pageFormElements = pageForm.getElementsByTagName('*');

var user_input;
    for (i=0;i<pageFormElements.length;i++) {
       if (pageFormElements[i].name == 'search_type' && pageFormElements[i].checked) 
        user_input = pageFormElements[i].value;       
      }

    swicth (user_input)
    {
          case 'email' :
                   add_validation('input', 'req', 'You need to enter an Email Address');
                   add_validation('input', 'email', 'Your Email seesm to be invalid');
          break;
         case 'name' :
                   add_validation('input', 'req', 'You need to enter a Mac ID');
                   add_validation('input', 'mac', 'Your MacID is invalid');
          break;
}


    </script>
That cleans up a few possible places where the script might fail due to undeclared variables etc..
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Dec 11th, 2007, 11:14
New Member
Join Date: Dec 2007
Location: Northern Ireland, UK
Age: 35
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Radio Buttons and Javascript

With your code i get

Error: Expected '}'

which i thought should be here:

for (i=0;i<pageFormElements.length;i++)
{
if (pageFormElements[i].name == 'search_type' && pageFormElements[i].checked)
{
user_input = pageFormElements[i].value;
}
}

But when i add that i still get Error: Expected '}'

Regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Dec 11th, 2007, 18:02
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Radio Buttons and Javascript

I have a typo, change swicth to switch
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
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

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] Help with Radio buttons Oak JavaScript Forum 17 Feb 2nd, 2008 09:44
How do i submit a form with radio buttons ? Accurax Web Page Design 5 May 12th, 2007 18:34
Radio buttons and mandatory fields joshcxa JavaScript Forum 1 Aug 8th, 2006 10:30
help with pagination and html radio buttons AdRock PHP Forum 5 Jul 27th, 2006 12:10
Radio Buttons redhead Web Page Design 2 Apr 12th, 2004 19:00


All times are GMT. The time now is 02:03.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

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