View Full Version : Adding Javascript to a PHP page not working...

06-05-2006, 11:52 PM
Could someone please tell me where about to put this code:
<form name="validation" onSubmit="return checkbae()">
Please input a valid email address:<br>
<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit">
<script language="JavaScript1.2">

var testresults
function checkemail(){
var str=document.validation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
alert("Please input a valid email address!")
return (testresults)

function checkbae(){
if (document.layers||document.getElementById||document.all)
return checkemail()
return true
</script> in this file? It's a php, just converted to .txt so you all can see what's going on... http://www.curiousclothing.com/index.txt

Cold Gunmetal
06-07-2006, 01:29 PM
checkbae? Bu then again, I don't know very much about JScript.

06-07-2006, 05:39 PM
Could someone please tell me where about to put this code:To be quite honest, the bin would be the best place (you should be happy about not having wrote it).

First, the alternative. Place the following in the head element (just before </head> will do):

<script type="text/javascript">
function validate(form) {
if (!isEmail(form.elements.emailcheck.value)) {
alert('Please enter a valid e-mail address.');
return false;
return true;

function isEmail(value) {
return /[^@]+@[^@.]+(\.[^@.]+)+/.test(value);
The form itself should look something like:

<form action="..." onsubmit="return validate(this);">
<label>E-mail address: <input name="emailcheck" value=""></label>
<input type="submit" value="Submit">

Now, for reasons to avoid what you posted.

<script language="JavaScript1.2">The language attribute has long been deprecated in favour of the type attribute. Worse still, you might actually get what you ask for: JavaScript v1.2. That particular version was very much different to versions before and since, and to actually have a script evaluated according to its rules could be quite a shock.

var testresults
function checkemail(){That variable declaration makes testresults a global variable. That shouldn't be at all desirable - there's need for it to be global - and little things like this brings the knowledge of the original author into doubt.

var str=document.validation.emailcheck.valueForm elements should be accessed using the forms collection, and controls within that form should be accessed using the elements collection of that form. Alternatively, one can do what I have above, and obtain a direct reference to the form within the event handler using the this operator. In any case, using the shortcut of access through the document object is a bad idea; that shortcut should never have been exposed by browsers in the first place.

var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/iThis regular expression is too restrictive and will reject valid e-mail addresses unnecessarily. Moreover, it uses non-capturing parentheses [&#160;(?:&#160;...&#160;)&#160;] for no apparent reason, which may cause syntax errors in older browsers. This is also quite perverse when one considers that the requested JavaScript version is 1.2; it never featured non-capturing parentheses.

If you're familiar with Perl-compatible regular expressions, you might wonder why my replacement is so simplistic (though, technically, even it will reject some valid addresses - e-mail addresses don't have to contain a dot in the right-hand side). The answer is simple: it's not possible to validate an e-mail address from the client. The only way to ensure that an address is valid is to attempt to send mail to it, and have the addressee do something upon receipt (send a reply or go to a unique confirmation URL). Sure, one can write a thorough syntactic check, but that doesn't prevent an authentic-looking address from being used.

<script>The type attribute is required.

function checkbae(){
if (document.layers||document.getElementById||document.all)This is, more-or-less, browser detection by object inference. Any time you see something like it, it's almost a sure bet that the author doesn't know what they're doing. Be careful, though: feature detection (sometimes referred to as object detection), which can look similar, but is usually more elaborate, shows quite the opposite - that the author is a good one.

Hope that helps,