Jump to content

[SOLVED] Javascript is driving me nuts. This is like the 20th version


simpli

Recommended Posts

Hi,

I've been trying to do a validation script for a form with textfields and radiobuttons. I've been having all sorts of problems as witnessed by my many postings. I admit at first I tried to do things too fast by just copy pasting. Since, I did the time to read some docs and further my knowledge. I have also cleaned my code (it was really messy) and decided to do a reboot and start with the field validations. Instead getelementbyid I am now using the document.form.object format. But still no luck. I have a very simple form with one text field that I want to validate. For some reason it doesn't work. I believe the function is called but that it doesnt get my field correctly because the validation doesnt run. Anyone can take a good look and tell me what I am doing wrong? I look at the code and I think it should be working but it's not.

 

<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields

var p1 = document.frmchoixronde1.player_1;


// Validation of the text fields!
if(notEmpty(p1, "Votre Joueur #1 n'a pas ete choisi")){
	return true;
}


return false;

}

function Empty(elem, helperMsg){
if(elem.value.length == 0){
	alert(helperMsg);
	elem.focus(); // set the focus to this input
	return false;
}
return true;
}

function radioSelected(radioGrp)
{

   if (radioGrp.length)
   {
      //There are 2 or more options
      for (var i=0; i<radioGrp.length; i++)
      {
         if (radioGrp[i].checked) { return true; }
      }
      //No options were checked
      return false;
   }
   else
   {
      //There is only 1 option
      return radioGrp.checked;
   }
}
</script>



<form id="frmchoixronde1" name="frmchoixronde1"  method="POST" 
enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'>
<html><head><title>Page des poolers</title></head> <body>
<b>Entrez vos choix pour la premiere ronde</b><menu class="one">hello jeanru<p></menu>


</select></fieldset></br></br><fieldset>
<b><label for="player_1" style="width:2em">1</label></b><input name="player_1" id="player_1" type="text" size="30"></br>

</fieldset><input type="submit" name="submit" value="Soumettre vos choix"></form>
</br> 
</body></html> 

function formValidator(){
   // Make quick references to our fields

   var p1 = document.frmchoixronde1.player_1;
   
   
   // Validation of the text fields!
   if(notEmpty(p1, "Votre Joueur #1 n'a pas ete choisi")){
      return true;
   }
      
   
   return false;
   
}

function Empty(elem, helperMsg){
   if(elem.value.length ===0){
      alert(helperMsg);
      elem.focus(); // set the focus to this input
      return false;
   }
   return true;
}

function radioSelected(radioGrp)
{

   if (radioGrp.length)
   {
      //There are 2 or more options
      for (var i=0; i<radioGrp.length; i++)
      {
         if (radioGrp[i].checked) { return true; }
      }
      //No options were checked
      return false;
   }
   else
   {
      //There is only 1 option
      return radioGrp.checked;
   }
}

Cleaned up more syntax was not valid and had  output functions before defining them  :P

 

Code:

 

function formValidator(){
   // Make quick references to our fields

   var p1 = document.frmchoixronde1.player_1;

   function Empty(elem, helperMsg){
   if(elem.value.length ===0){
      alert(helperMsg);
      elem.focus(); // set the focus to this input
      return false;
   }

   // Validation of the text fields!
   if(!Empty(p1, "Votre Joueur #1 n'a pas ete choisi")){
      return true;
   }


   return false;

}


   return true;
}

function radioSelected(radioGrp)
{

   if (radioGrp.length)
   {
      //There are 2 or more options
      for (var i=0; i<radioGrp.length; i++)
      {
         if (radioGrp[i].checked) { return true; }
      }
      //No options were checked
      return false;
   }
   else
   {
      //There is only 1 option
      return radioGrp.checked;
   }
}

I'm on a mac and using MAMP. I dont know how to access the error console. Any clue?

J-R

You errors appear in your browser not your server. It doesn't matter what OS you have. Just get firefox and use the firebug plugin to debug easier

I'm on a mac and using MAMP. I dont know how to access the error console. Any clue?

J-R

 

Safari also comes with a built in javascript console. I couldn't tell ya how to get to it, but it shouldn't be hard to do.

 

http://www.jslint.com/

That web site can check your js code. It's owned/run by a Yahoo JavaScript Architect and inventor of JSON. This is a very recommended site.

Guys thank you all,

I was already on Firefox so i just fired up my console and were able to see my errors. It's much better that way. Most of them were corrected easily with the help of the console. Without the console it was a bit like flying blind. Especially since I was new to js. Thank you all very much.

J-R

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.