Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following java script function in my perl document:

function emailcheck()
{
  var emailoutline = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
  var x = document.ec.email.value;
  if (emailoutline.test(x))
  {
    alert("valid email");  
  }
}

that is part of a print html block so I think it should work fine. The html part works fine if I use it outside of the perl document, but once I link it there, it does not work anymore. The "@" in the regular expression is also marked green while all the rest of the html part of the script is marked red - I think the problem is that the "@" is interpreted by perl as the beginning of an array. Does anybody have an idea how to fix this problem?

This is the whole code:

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";
print <<'HTML';
<html>
<head>
<title>Mailcheck</title>
</head>
<body>
  <form name="ec" action ="">
  Email: <input type="text" name="email"> <br>
  <input type="button" value="Pruefen" onclick="javascript:emailcheck();">
  </form>
  <script language="javascript" type="text/javascript">
  function emailcheck()
  {
    var emailoutline = /^[a-z0-9._%+-]+\@[a-z0-9.-]+\.[a-z]{2,4}$/;
    var x = document.ec.email.value;
    if (emailoutline.test(x))
    {
      alert("This is a valid eMail");  
    }
    else
    {
      alert("This is not a valid eMail");
    }
  }
  </script>
 </body>
HTML
exit; 
share|improve this question
1  
Escape means putting a backslashe before the character, like \@. In regex we can also express characters by its ascii code, you can also try using \x40 instead of @ (40 is the HEX equivalent to "@" in ascii table) –  Caio Oliveira May 19 at 13:32
    
I have now put in the whole code - the edits you suggested solved the problem, that the "@" is not marked in green anymore but the code is still not working - Maybe the problem is somewhere else? –  eva May 19 at 13:57
    
Perhaps it's better to put the JavaScript into a separate file. –  reinierpost May 20 at 7:04
add comment

3 Answers 3

I solved the problem on my own:

I had to rewrite the regular expression like this:

var emailoutline = /^[a-z0-9._%+-]+\@[a-z0-9.-]+\.[a-z]{2,4}\$\/\;

since perl cut of the reserved symbols "$" and "/". Now the code is working fine - Just wanted to post the solution if someone else ever comes across the same problem. Thank you for all the help though :)

share|improve this answer
add comment

Use a non-interpolating print to prevent Perl from attempting to expand variables:

print <<'HTML';
...
HTML
share|improve this answer
add comment

In Perl, @ is a reserved character. You need to escape it (\@) to avoid being interpreted as an array sigil.

share|improve this answer
    
Thanks for the post, however, it is still not working so I posted the whole code above - maybe the problem is somewhere else? –  eva May 19 at 13:59
    
oops that just got missing due to copy pasting from my file... thanks for pointing it out though. it did not solve the problem sadly :/ –  eva May 19 at 14:10
    
As you can see from the example link I posted, I copy and pasted your code and it works –  hwnd May 19 at 14:11
    
Well, thats the problem. The html code alone also works for me - it just does not work when linked in perl document like i did it above... –  eva May 19 at 14:12
    
Do you have the right properties set on your script? –  hwnd May 19 at 14:15
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.