Web Design and Development Forums

[SOLVED] help with checkbox and textfield in form

This is a discussion on "[SOLVED] help with checkbox and textfield in form" within the JavaScript Forum section. This forum, and the thread "[SOLVED] help with checkbox and textfield in form 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 21st, 2008, 18:02   #1 (permalink)
Junior Member
 
Join Date: Apr 2007
Location: Kansas
Age: 18
Posts: 33
Send a message via AIM to BRONIC
[SOLVED] help with checkbox and textfield in form

I am having trouble in getting this form to work.
i am trying to make sure that if the 'other' checkbox is checked and the other textbox is empty then an alert tells the user they need to fill in the box, and when the box is not checked it does nothing.


here is my form:
HTML: Select all
<script type="text/javascript">
function showHide(id){ 
   el = document.getElementById(id); 
   el.style.display = (el.style.display != 'block')? 'block' : 'none';
}
</script>

<form name="change" action="resize.php?action=change" id="change" method="POST">
	<p>Your Style: <input type='text' 	name='style'></p>
	<p>Your Bio(less than 300 words):<br><textarea name="bio" cols="100" rows"10"></textarea></p>
	<p>What categories would you like to display?<br>
	<Input type = 'Checkbox' Name ='web' value= '1'> Websites<br>
	<Input type = 'Checkbox' Name ='art' value= '1'> Traditional Artwork<br>
	<Input type = 'Checkbox' Name ='ani' value= '1'> Animations<br>
	<Input type = 'Checkbox' Name ='vid' value= '1'> Video<br>
	<Input type = 'Checkbox' Name ='pho' value= '1'> Photography<br>
	<Input type = 'Checkbox' Name ='mus' value= '1'> Music<br>
	<input name ='oth' type = 'Checkbox' onClick="showHide('other_text')" value= '1'> 
	Other<br>
    <div id="other_text" style="display:none">If other, please specify:
      <input type='text' name='other'></div>
    <p>
	</p>
	Choose a background color:<br>
	<Input type = 'Radio' Name ='color' value= 'red'> Red<br>
	<Input type = 'Radio' Name ='color' value= 'blue'> Blue<br>
	<Input type = 'Radio' Name ='color' value= 'green'> Green<br>
	<Input type = 'Radio' Name ='color' value= 'black'> Black<br>
	<Input type = 'Radio' Name ='color' value= 'grey'> Grey<br>
	<Input type = 'Radio' Name ='color' value= 'orange'> Orange<br>
	<Input type = 'Radio' Name ='color' value= 'purple'> Purple<br>
	<Input type = 'Radio' Name ='color' value= 'yellow'> Yellow<br>
  <p><input type='submit' name='Submit' value='Update Page' onClick=""></p>
</form>

here is the link to the page:
here
BRONIC 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 21st, 2008, 20:03   #2 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: help with checkbox and textfield in form

<input type='submit' onclick="{PUT YOUR VALIDATION HERE}"
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?
spinal007 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 21st, 2008, 20:25   #3 (permalink)
Junior Member
 
Join Date: Apr 2007
Location: Kansas
Age: 18
Posts: 33
Send a message via AIM to BRONIC
Re: help with checkbox and textfield in form

Well i know that but im having trouble checking that the box is checked AND the text field is filled in.
BRONIC 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 21st, 2008, 20:30   #4 (permalink)
Chief Moderator
 
aso186's Avatar
 
Join Date: Oct 2007
Location: UK
Posts: 715
Blog Entries: 2
Send a message via Skype™ to aso186
Re: help with checkbox and textfield in form

Gather all the checkboxes in an array and add the values. If the total is equal to zero, no checkbox was selected. For the text field, just check the value length is greater than or equal to one.
Code: Select all
if (checkboxes.total == 0 || textfield.value <= 1) {
    return false;
}
__________________

aso186 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 21st, 2008, 21:15   #5 (permalink)
Junior Member
 
Join Date: Apr 2007
Location: Kansas
Age: 18
Posts: 33
Send a message via AIM to BRONIC
Re: help with checkbox and textfield in form

Quote:
Originally Posted by aso186 View Post
Gather all the checkboxes in an array and add the values. If the total is equal to zero, no checkbox was selected. For the text field, just check the value length is greater than or equal to one.
Code: Select all
if (checkboxes.total == 0 || textfield.value <= 1) {
    return false;
}
yes but then if any of the check boxes were checked then you would have to fill out the text field, but it is only shown when the 'other' chkbox is checked.
BRONIC 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 22nd, 2008, 08:14   #6 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: help with checkbox and textfield in form

- Add an id to your checkbox:
<input name='oth' id='oth'

- Add an id to your textbox:
<input type='text' name='other' id='other'

- Use this to checked whether the 'other' textbox is selected:
document.getElementById('oth').checked

- Slight change to aso's suggestion, which will work on if textfield.value is empty or null
(textfield.value || '').length <= 0

- Another change to aso's suggestion: checked AND empty (not OR)

- Then you end up with this:
Code: Select all
if(document.getElementById('oth').checked && (document.getElementById('other').value || '').length <= 0) {
 return false;
}
You should be able to make this work now...
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?
spinal007 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 22nd, 2008, 15:26   #7 (permalink)
Junior Member
 
Join Date: Apr 2007
Location: Kansas
Age: 18
Posts: 33
Send a message via AIM to BRONIC
Re: help with checkbox and textfield in form

HTML: Select all
<script type="text/javascript">
function showHide(id){ 
   el = document.getElementById(id); 
   el.style.display = (el.style.display != 'block')? 'block' : 'none';
}
function check(form)
    if(document.getElementById('other_check').checked && (document.getElementById('other_textbox').value || '').length <= 0) {
         alert('The box labeled "Other" was checked but you didn\'t enter a category into the text box');
    else {
        form.submit();
    }
}
</script>

<form name="change" action="resize.php?action=change" id="change" method="POST">
    <p>Your Style: <input type='text'     name='style'></p>
    <p>Your Bio(less than 300 words):<br><textarea name="bio" cols="70" rows"50"></textarea></p>
    <p>What categories would you like to display?<br>
    <Input type = 'Checkbox' Name ='web' value= '1'> Websites<br>
    <Input type = 'Checkbox' Name ='art' value= '1'> Traditional Artwork<br>
    <Input type = 'Checkbox' Name ='ani' value= '1'> Animations<br>
    <Input type = 'Checkbox' Name ='vid' value= '1'> Video<br>
    <Input type = 'Checkbox' Name ='pho' value= '1'> Photography<br>
    <Input type = 'Checkbox' Name ='mus' value= '1'> Music<br>
    <input name ='oth' type = 'Checkbox' id="other_check" onClick="showHide('other_text')" value= '1'> 
    Other<br>
    <div id="other_text" style="display:none">If other, please specify:
      <input type='text' id="other_textbox" name='other'></div>
    <p>
    </p>
    Choose a background color:<br>
    <Input type = 'Radio' Name ='color' value= 'red'> Red<br>
    <Input type = 'Radio' Name ='color' value= 'blue'> Blue<br>
    <Input type = 'Radio' Name ='color' value= 'green'> Green<br>
    <Input type = 'Radio' Name ='color' value= 'black'> Black<br>
    <Input type = 'Radio' Name ='color' value= 'grey'> Grey<br>
    <Input type = 'Radio' Name ='color' value= 'orange'> Orange<br>
    <Input type = 'Radio' Name ='color' value= 'purple'> Purple<br>
    <Input type = 'Radio' Name ='color' value= 'yellow'> Yellow<br>
  <p><input type='submit' name='Submit' value='Update Page' onClick="try{ check(this.form); }catch(e){}; return false;"></p>
</form>

Last edited by spinal007; Apr 22nd, 2008 at 15:59.
BRONIC 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 22nd, 2008, 15:58   #8 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: help with checkbox and textfield in form

I'm sorry, I edited your post when I meant to copy your code. Bad moderator! bad!

Here's what I think you should have:
HTML: Select all
<script type="text/javascript">
function showHide(id){ 
   el = document.getElementById(id); 
   el.style.display = (el.style.display != 'block')? 'block' : 'none';
}
function check(form)
    if(document.getElementById('other_check').checked && (document.getElementById('other_textbox').value || '').length <= 0) {
         alert('The box labeled "Other" was checked but you didn\'t enter a category into the text box');
    else {
        form.submit();
    }
}
</script>

<form name="change" action="resize.php?action=change" id="change" method="POST">
    <p>Your Style: <input type='text'     name='style'></p>
    <p>Your Bio(less than 300 words):<br><textarea name="bio" cols="70" rows"50"></textarea></p>
    <p>What categories would you like to display?<br>
    <Input type = 'Checkbox' Name ='web' value= '1'> Websites<br>
    <Input type = 'Checkbox' Name ='art' value= '1'> Traditional Artwork<br>
    <Input type = 'Checkbox' Name ='ani' value= '1'> Animations<br>
    <Input type = 'Checkbox' Name ='vid' value= '1'> Video<br>
    <Input type = 'Checkbox' Name ='pho' value= '1'> Photography<br>
    <Input type = 'Checkbox' Name ='mus' value= '1'> Music<br>
    <input name ='oth' type = 'Checkbox' id="other_check" onClick="showHide('other_text')" value= '1'> 
    Other<br>
    <div id="other_text" style="display:none">If other, please specify:
      <input type='text' id="other_textbox" name='other'></div>
    <p>
    </p>
    Choose a background color:<br>
    <Input type = 'Radio' Name ='color' value= 'red'> Red<br>
    <Input type = 'Radio' Name ='color' value= 'blue'> Blue<br>
    <Input type = 'Radio' Name ='color' value= 'green'> Green<br>
    <Input type = 'Radio' Name ='color' value= 'black'> Black<br>
    <Input type = 'Radio' Name ='color' value= 'grey'> Grey<br>
    <Input type = 'Radio' Name ='color' value= 'orange'> Orange<br>
    <Input type = 'Radio' Name ='color' value= 'purple'> Purple<br>
    <Input type = 'Radio' Name ='color' value= 'yellow'> Yellow<br>
  <p><input type='submit' name='Submit' value='Update Page' onClick="try{ check(this.form); }catch(e){}; return false;"></p>
</form>
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?
spinal007 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 22nd, 2008, 16:08   #9 (permalink)
Junior Member
 
Join Date: Apr 2007
Location: Kansas
Age: 18
Posts: 33
Send a message via AIM to BRONIC
Re: help with checkbox and textfield in form

Quote:
Originally Posted by spinal007 View Post
I'm sorry, I edited your post when I meant to copy your code. Bad moderator! bad!
Thats ok, thanks for the help. it worked great.

Thanks
-BRONIC
BRONIC 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

Tags
blah, bronic, checkbox, form, java, php

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
CSS form checkbox alignment! dhossai CSS Forum 4 Nov 29th, 2007 18:00
[SOLVED] Form Checkbox Validation Stuart PHP Forum 26 Nov 24th, 2007 23:39
[SOLVED] Form to email with checkbox itsdesign PHP Forum 4 Oct 29th, 2007 00:57
A Solved Checkbox snow Webforumz Suggestions and Feedback 14 Sep 25th, 2007 20:11
Desparately Need Form, Checkbox Component Help grillbat New to Web Design 1 Aug 21st, 2007 16: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:21.

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