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> 

Link to comment
Share on other sites

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;
   }
}

Link to comment
Share on other sites

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;
   }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.