radio button validation

  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post June 30th, 2004, 5:29 am

i have this validation for a textbox/textfield:

Code: [ Select ]
function checkform ( form )
{
 if (form.name.value == "") {
  alert( "Please enter your name." );
  form.name.focus();
  return false ;
 }
  1. function checkform ( form )
  2. {
  3.  if (form.name.value == "") {
  4.   alert( "Please enter your name." );
  5.   form.name.focus();
  6.   return false ;
  7.  }


how can i edit this script to validate a radio button?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 30th, 2004, 5:29 am

  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 6:40 am

Code: [ Select ]
if (document.FORMNAME.BUTTONNAME.checked == true){
   BLAH BLAH BLAH
}
  1. if (document.FORMNAME.BUTTONNAME.checked == true){
  2.    BLAH BLAH BLAH
  3. }
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post June 30th, 2004, 7:04 am

all the other alerts for the text fields are in the form of
Code: [ Select ]
if (form.name.value == "") { //etc
if (form.email.value == "") { //etc
if (form.mobile.value == "") { //etc
  1. if (form.name.value == "") { //etc
  2. if (form.email.value == "") { //etc
  3. if (form.mobile.value == "") { //etc



so this code is a lil different
Code: [ Select ]
if (document.FORMNAME.BUTTONNAME.checked == true){
   BLAH BLAH BLAH
}
  1. if (document.FORMNAME.BUTTONNAME.checked == true){
  2.    BLAH BLAH BLAH
  3. }


i tried changing it to
Code: [ Select ]
if (!document.FORM.doyouneed.checked == true){
   BLAH BLAH BLAH
}
  1. if (!document.FORM.doyouneed.checked == true){
  2.    BLAH BLAH BLAH
  3. }

but it didnt work.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 7:13 am

Well if you want it to work you have to add an alert like this

Code: [ Select ]
   if (document.formname.bottonname.checked == true{
   alert('Whatever you want it to say here');
}
  1.    if (document.formname.bottonname.checked == true{
  2.    alert('Whatever you want it to say here');
  3. }
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post June 30th, 2004, 7:17 am

thing is it doesnt work at all. the format is different to the other validation statements
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 7:36 am

ok...are you telling me the name of your form is "form"?
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 7:47 am

Quote:
Code: [ Select ]
if (!document.FORM.doyouneed.checked == true){
   BLAH BLAH BLAH
}
  1. if (!document.FORM.doyouneed.checked == true){
  2.    BLAH BLAH BLAH
  3. }



ok...are you telling me the name of your form is "form"?
also don't need the !
You do realize that this tiny lil code will alert whatever you tell it to. Once you click the radio button and have something call this code.

I know this works...I actually stole the code froma validation I used before. Maybe try running it in a separate function...just so you can see it works. Maybe there's something bugging up when you try to add this to your previous validation function.

once again:
Code: [ Select ]
if(document.formname.radiobuttonname.checked == true){
   alert("Put your alert here.");
}
  1. if(document.formname.radiobuttonname.checked == true){
  2.    alert("Put your alert here.");
  3. }

if you insert your form name and radio button name this should work like a champ
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 7:56 am

if this doesn't work for you, post your entire validation function and I'll work on it
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 30th, 2004, 8:11 am

If a radio button is selected, the value of its checked property is true; otherwise, it is false. You can set the checked property at any time. The display of the radio button updates immediately when you set the checked property.

At any given time, only one button in a set of radio buttons can be checked. When you set the checked property for one radio button in a group to true, that property for all other buttons in the group becomes false.

Examples
The following example examines an array of radio buttons called musicType on the musicForm form to determine which button is selected. The VALUE attribute of the selected button is assigned to the checkedButton variable.
Code: [ Select ]
function stateChecker() { 
   var checkedButton = "" 
   for (var i in document.musicForm.musicType) {   
     if (document.musicForm.musicType[i].checked=="1"){
        checkedButton=document.musicForm.musicType[i].value 
     }
   }
}
  1. function stateChecker() { 
  2.    var checkedButton = "" 
  3.    for (var i in document.musicForm.musicType) {   
  4.      if (document.musicForm.musicType[i].checked=="1"){
  5.         checkedButton=document.musicForm.musicType[i].value 
  6.      }
  7.    }
  8. }
Strong with this one, the sudo is.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 8:15 am

mine was an array to...I just took the array parts out...b/c I think buzzby just wanted to check one thing...maybe not though...we'll see
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post June 30th, 2004, 8:30 am

Code: [ Select ]
</SCRIPT>
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
 if (form.name.value == "") {
  alert( "Please enter your name." );
  form.name.focus();
  return false ;//this works because its a textfield
 }
if (form.email.value == "") {
  alert( "Please enter your email address." );
  form.email.focus();
  return false ;//this works because its a textfield
 }
if (form.contactaddress.value == "") {
  alert( "Please enter your contact address." );
  form.contactaddress.focus();
  return false ;//this works because its a textfield
 }

if (form.doyouneed.checked == true){
alert( "Please check the box concerning your needs." );
return false; //this doesnt work:radio button
}
if (form.designstyle.value == "") {
  alert( "Please enter your preference for design style." );
  form.designstyle.focus();
  return false ;//this doesnt work:radio button
 }
if (form.project.value == "") {
  alert( "Please enter the the type of project." );
  form.project.focus();
  return false ;//this doesnt work:radio button
 }
if (form.uses.value == "") {
  alert( "Please enter the type of use." );
  form.uses.focus();
  return false ;//this doesnt work:radio button
 }
 return true;
}//even when i tried it with "if(document.formname.radiobuttonname.checked == true){
   alert("Put your alert here."); " it still didnt work. different architecture
}
</script>
  1. </SCRIPT>
  2. <script language="JavaScript" type="text/javascript">
  3. function checkform ( form )
  4. {
  5.  if (form.name.value == "") {
  6.   alert( "Please enter your name." );
  7.   form.name.focus();
  8.   return false ;//this works because its a textfield
  9.  }
  10. if (form.email.value == "") {
  11.   alert( "Please enter your email address." );
  12.   form.email.focus();
  13.   return false ;//this works because its a textfield
  14.  }
  15. if (form.contactaddress.value == "") {
  16.   alert( "Please enter your contact address." );
  17.   form.contactaddress.focus();
  18.   return false ;//this works because its a textfield
  19.  }
  20. if (form.doyouneed.checked == true){
  21. alert( "Please check the box concerning your needs." );
  22. return false; //this doesnt work:radio button
  23. }
  24. if (form.designstyle.value == "") {
  25.   alert( "Please enter your preference for design style." );
  26.   form.designstyle.focus();
  27.   return false ;//this doesnt work:radio button
  28.  }
  29. if (form.project.value == "") {
  30.   alert( "Please enter the the type of project." );
  31.   form.project.focus();
  32.   return false ;//this doesnt work:radio button
  33.  }
  34. if (form.uses.value == "") {
  35.   alert( "Please enter the type of use." );
  36.   form.uses.focus();
  37.   return false ;//this doesnt work:radio button
  38.  }
  39.  return true;
  40. }//even when i tried it with "if(document.formname.radiobuttonname.checked == true){
  41.    alert("Put your alert here."); " it still didnt work. different architecture
  42. }
  43. </script>
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 20, 2004
  • Posts: 524
  • Loc: NC, USA
  • Status: Offline

Post June 30th, 2004, 8:40 am

Code: [ Select ]
if (form.radiobuttonname.checked == true{
   alert("Whatever");
   form.radiobuttonnme.focus();
   return false;
}
  1. if (form.radiobuttonname.checked == true{
  2.    alert("Whatever");
  3.    form.radiobuttonnme.focus();
  4.    return false;
  5. }


make sure to insert the name of your radio button not radiobuttonname
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post June 30th, 2004, 8:50 am

doesnt work at all
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post June 30th, 2004, 9:27 am

ahhh...

managed to fix it. this code works:

Code: [ Select ]
if (!(mainform.designstyle[0].checked || mainform.designstyle[1].checked))
        {
        alert('Your design style is a required field. Please try again.');
        event.returnValue=false;
        }
  1. if (!(mainform.designstyle[0].checked || mainform.designstyle[1].checked))
  2.         {
  3.         alert('Your design style is a required field. Please try again.');
  4.         event.returnValue=false;
  5.         }


and this is in the form field:
Code: [ Select ]
<form method="POST" name="mainform" action="getstartedform.php" onsubmit="validate();">


works fine and in the way i want it to work. individual validation without refreshing the form.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 30th, 2004, 6:02 pm

s15199d wrote:
mine was an array to...I just took the array parts out...b/c I think buzzby just wanted to check one thing...maybe not though...we'll see


I was only trying to show the normal use of radio buttons. Though visual preference varies from person to person, checkboxes were meant for single choices while radio buttons/groups are meant for multiple choice.
Sorry if you feel I stepped on your toes, that was not my intent.
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 30th, 2004, 6:02 pm

Post Information

  • Total Posts in this topic: 16 posts
  • Users browsing this forum: No registered users and 201 guests
  • 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 post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.