d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 100 Fg To Whoever Fixes This Javascript Problem
12Next
Add Reply New Topic New Poll
Member
Posts: 9,412
Joined: Nov 18 2009
Gold: 20.00
Aug 6 2012 12:47am
Do not Just Post the fixed version if you do figure out the problem. Explain it.

The Javascript:

Code
function validateForm(contact) {
   var errReport = "";
   errReport += validName(contact.name);
   errReport += validEmail(contact.email);
   errReport += validMessage(contact.message);
   errReport += validHuman(contact.human);
   errReport += validSubject(contact.subject);
   if (errReport != "") {
   
   return false;
   }
 return true;
 }

  function validName(name) {
  var error = "";
  var valid = /^[a-zA-Z]/;
   
  if (name.value == "") {
  name.style.borderColor = '#ff0000';
  document.getElementById('name').style.color = "#ff0000";
  error = " - You forgot to enter a name.\n";
  } else if(valid.test(name.value)){
  name.style.borderColor = '';
  document.getElementById('name').style.color = "";
  } else {
  name.style.borderColor = '#ff0000';
  document.getElementById('name').style.color = "#ff0000";
  error = " - Your User name can only be A-Z and 5-15 characters.\n";
  }
  return error;
  }

  function validEmail(email) {
  var error = "";
  var valid = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
 
  if (email.value == "") {
  email.style.borderColor = '#ff0000';
  document.getElementById('email').style.color = "#ff0000";
  error = " - You forgot to enter an email address.\n";
  } else if(valid.test(email.value)){
  email.style.borderColor = '';
  document.getElementById('email').style.color = "";
  } else {
  email.style.borderColor = '#ff0000';
  document.getElementById('email').style.color = "#ff0000";
  error = " - That was not a valid email address.\n";
  }
  return error;
  }

  function validMessage(message) {
  var error = "";
 
  if (message.value == "") {
  message.style.borderColor = '#ff0000';
  document.getElementById('message').style.color = "#ff0000";
  error = " - You forgot your message.\n";
  } else{
  message.style.borderColor = '';
  document.getElementById('message').style.color = "";
  }
  return error;
  }

  function validSubject(subject) {
  var error = "0";
  var valid = /^[1-9_.-]/;
 
  if (subject.value == "0") {
 subject.style.borderColor = '#ff0000';
 document.getElementById('subject').style.color = "#ff0000";
 error = " - Pick One.\n";
  } else if(valid.test(subject.value)){
 subject.style.borderColor = '';
 document.getElementById('subject').style.color = "";
  }else{
 subject.style.borderColor = '';
 document.getElementById('subject').style.color = "";
 }
  return error;
  }

  function validHuman(human) {
 var error = "";
 
 
 if (human.value != "8") {
  human.style.borderColor = '#ff0000';
  document.getElementById('human').style.color = "#ff0000";
  error = " - The answer is 8 dummy.\n";
 } else{
  human.style.borderColor = '';
  document.getElementById('human').style.color = "";
 
 
  }
  return error;
  }



The Html form:

Code
<form action="contact.php" method="post" name="contact" onsubmit="return validateForm(this);" >
   <label>Name</label>
   <input type="text" name="name" id="name">
   <label>Email</label>
   <input type="text" name="email" id="email" >
   <label>Subject</label>
   <select name="subject" size="1" id="subject">
   <option name="option0" value="0">Choose One</option>
   <option name="option1" value="1">A</option>
   <option name="option2" value="2">B</option>
   <option name="option3" value="3">C</option>
   <option name="option4" value="4">D</option>
   <option name="option5" value="5">E</option>
   <option name="option6" value="6">F</option>
   </select>
   <label>Description</label>
   <textarea name="message" id="message"></textarea>
   <label>*What is 4+4? (Anti-spam)</label>
   <input type="text" name="human" id="human">
   <input type="submit" value="submit" name="send form" id="submit1" class="button1" >
</form>


This post was edited by PixileDust on Aug 6 2012 01:12am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 6 2012 12:48am
can you describe what the problem you're experiencing is?
Member
Posts: 9,412
Joined: Nov 18 2009
Gold: 20.00
Aug 6 2012 12:54am
Quote (carteblanche @ Aug 6 2012 12:48am)
can you describe what the problem you're experiencing is?


Yes, sorry.

When I press the submit button on the form, the validator works, I can tell because the border becomes red on the text fields. However, it doesn't go through with the action(quote.php).

I know the problem is the validator, because when I remove it or change the validate form function

from this:

Code
function validateForm(contact) {
  var errReport = "";
  errReport += validName(contact.name);
  errReport += validEmail(contact.email);
  errReport += validMessage(contact.message);
  errReport += validHuman(contact.human);
  errReport += validSubject(contact.subject);
  if (errReport != "") {
 
  return false;
  }
return true;
}


to this:

Code
function validateForm(contact) {
  var errReport = "";
  errReport += validName(contact.name);
  errReport += validEmail(contact.email);
  errReport += validMessage(contact.message);
  errReport += validHuman(contact.human);
  errReport += validSubject(contact.subject);
  if (errReport == "") {
 
  return false;
  }
return true;
}


then Quote.php will run, but then of course the validator doesn't work correctly..

What do I have to change to make it run correctly?

Let me know if I need to explain anything else

This post was edited by PixileDust on Aug 6 2012 01:02am
Member
Posts: 9,412
Joined: Nov 18 2009
Gold: 20.00
Aug 6 2012 01:07am
To put it simply, when I press the submit button, the border turns red on some of the fields(so that part works), but even after you've corrected the problems, such as having an email without an @, it still doesn't submit..


Quote.php is just this currently:

echo '<p>success</p>'

so that's obviously not the problem..

This post was edited by PixileDust on Aug 6 2012 01:08am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 6 2012 01:30am
im not very experienced in js and im not gonna run/debug your code, so i could be wrong. but this looks suspicious to me

Quote
function validSubject(subject) {
var error = "0";
  var valid = /^[1-9_.-]/;
// blah blah blah
  return error;
  }


looks like if it IS a valid subject, it'll return "0" which is not ""

it fits your symptoms. the code that turns stuff red isn't executed (since the if block for the errors is false), but it doesn't submit the form because it's not "" so nothing happens.

if that doesn't solve your problem, i recommend debugging/logging the result of each individual function to see what it returns. also debug/log the value of errReport right before you return. once you narrow down which line is problematic, it should be easier to fix

This post was edited by carteblanche on Aug 6 2012 01:39am
Member
Posts: 9,412
Joined: Nov 18 2009
Gold: 20.00
Aug 6 2012 10:54am
Quote (carteblanche @ Aug 6 2012 01:30am)
im not very experienced in js and im not gonna run/debug your code, so i could be wrong. but this looks suspicious to me



looks like if it IS a valid subject, it'll return "0" which is not ""

it fits your symptoms. the code that turns stuff red isn't executed (since the if block for the errors is false), but it doesn't submit the form because it's not "" so nothing happens.

if that doesn't solve your problem, i recommend debugging/logging the result of each individual function to see what it returns. also debug/log the value of errReport right before you return. once you narrow down which line is problematic, it should be easier to fix


Wow, one simple mistake.. fg is sent.


It took me 30 minutes to write the code and 3 hours trying to figure out the problem..

This post was edited by PixileDust on Aug 6 2012 10:54am
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Aug 7 2012 09:34am
Quote (PixileDust @ Aug 6 2012 02:54pm)
Wow, one simple mistake.. fg is sent.


It took me 30 minutes to write the code and 3 hours trying to figure out the problem..


That is pretty common in programming, don't worry =)

Member
Posts: 13
Joined: Aug 11 2012
Gold: 0.00
Aug 11 2012 11:05am
Quote (PixileDust @ Aug 6 2012 12:54pm)
Wow, one simple mistake.. fg is sent.


It took me 30 minutes to write the code and 3 hours trying to figure out the problem..


happens to me all the time. I recently started using matlab and it is more user friendly so you can debug each line of code to see where the error is.

edit: new to programming so don't mind me ;)

This post was edited by We1cher on Aug 11 2012 11:06am
Member
Posts: 13,028
Joined: Oct 24 2008
Gold: 0.00
Aug 19 2012 01:40pm
Quote (StormHasHe @ Aug 7 2012 08:34am)
That is pretty common in programming, don't worry =)


y, true
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Aug 23 2012 11:25pm
Quote (PixileDust @ 6 Aug 2012 09:54)
Wow, one simple mistake.. fg is sent.


It took me 30 minutes to write the code and 3 hours trying to figure out the problem..


that's because you're trying to reinvent the wheel
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll