0

I have the following script that works fine until I add the other javascript below...

First Script in the header tags

function validateForm() {   
    var valid = true;
    var errMsg = "";
    var email = document.emailform.email.value;
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

   if(email.length <= 0) {
      valid = false;
      errMsg += "Email address is required.\n";
   } else {
      if (!filter.test(email)) {
         valid = false;
         errMsg += "Please provide a valid email address.\n";
      }
   }   
   if (errMsg.length > 0) {
      alert(errMsg);
      return false;
   }
}

and just before the closing tags I have...

$('#form').submit(validateForm);

The above works fine, except once I add the script below, validateForm no longer works. This following is added just before the closing body tags.

cbr202=Math.random()*10000000000000000;document.write('<scr'+'ipt language="JavaScript" src="http://example.com/landing.php?lpip=411&202cb='+cbr202+'" type="text/javascript"></scr' + 'ipt>');

I can't seem to figure out what's causing the issue. Hoping someone with experience can see the problem.


Solved: I figured it out... it was due to my own sloppiness. I should have the jquery event handler below the document.write script, not above it.

6
  • What is the conflict? Check your console for any error messages. Commented May 24, 2012 at 21:47
  • that second line writes out a link to another javascript file. What's in THAT javascript file? Commented May 24, 2012 at 21:48
  • @JonathanSampson That's the weird thing, there are no console error messages. Commented May 24, 2012 at 23:44
  • @user1373779 What then do you mean by conflict? Commented May 25, 2012 at 0:06
  • @JonathanSampson Maybe that's not proper terminology, but the first script works fine on it's own. Once I add the second script, it stops working. The second script works regardless. Commented May 25, 2012 at 0:35

3 Answers 3

1

You forgot to add a closing } to the function. That caused an error and resulted in any JS coming after that to not execute.

function validateForm() {   
    var valid = true;
    var errMsg = "";
    var email = document.emailform.email.value;
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

   if(email.length <= 0) {
      valid = false;
      errMsg += "Email address is required.\n";
   } else {
      if (!filter.test(email)) {
         valid = false;
         errMsg += "Please provide a valid email address.\n";
      }
   }   
   if (errMsg.length > 0) {
      alert(errMsg);
      return false;
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Shouldn't it then already fail without the second script? I guess something else is wrong here.
Fair, but I just corrected what I saw. I think he thinks the first part works because he didn't test it, which he only did once he added the second part of the script. Just my guess though. But fair critique.
There's definitely something he/she is not telling us. The second part is perfectly valid.
whoops... yeah, I forgot to copy/paste the closing }. It's in the original script though.
@user1373779 : Did you retry with the new closed bracket code?
|
0

the url generated by src="http://mysite.com/landing.php?lpip=411&202cb='+cbr202 does not exist. The browser tries to load the script from the url using a get request and fails.

3 Comments

You do know mysite.com is a placeholder for this question.
As Stanislav said, it's just a placeholder. The URL does exist, and that script is working. The issue is with the validateForm function, which works until the next script is added.
i know mysite.com is a placeholder... i am talking about the complete url which has a random number just generated as a part of it... the complete url can never exist beforehand
0
cbr202=Math.random()*10000000000000000;

I think you need change code same below

var cbr202=Math.random()*10000000000000000;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.