Jump to content

A email validation script isn't working!


clonetrooper9494

Recommended Posts

I made a script that checks the value of 2 things and if they don't match, it displays errors. I found a short script from a site that I can't get working, but it works on their page. I copied the script in to a new page, and it worked. then I tried to move it into a registering page with other stuff... but it stopped working. Did I do something wrong when trying to move it and change the id's in the script?New Page source:

<!-- TWO STEPS TO INSTALL EMAIL VALIDATION - BASIC:  1.  Copy the coding into the HEAD of your HTML document  2.  Add the last code into the BODY of your HTML document  --><!-- STEP ONE: Paste this code into the HEAD of your HTML document  --><HEAD><script LANGUAGE="JavaScript"><!-- This script and many more are available free online at --><!-- The JavaScript Source!! http://javascript.internet.com --><!-- Beginfunction checkEmail(myForm) {if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this.emailer.value)){return (true)}alert("Invalid E-mail Address! Please re-enter.")return (false)}//  End --></script></HEAD><!-- STEP TWO: Copy this code into the BODY of your HTML document  --><BODY><form onSubmit="return checkEmail(this)">E-mail Address:<br><input type="text" name="emailer"><p><input type="submit" value="Submit"><input type="reset" value="Reset"></form><p><center><font face="arial, helvetica" size"-2">Free JavaScripts provided<br>by <a href="http://javascriptsource.com">The JavaScript Source</a></font></center><p><!-- Script Size:  1.07 KB -->

Register Page source:

<html><head><title>Redister at ***************</title><script LANGUAGE="JavaScript">function validation(form_var) {v_email();v_pass();v_email2();}function v_email() {if (document.getElementById('e1').value!=document.getElementById('e2').value){document.getElementById('e_invalid').innerHTML = "'Confirm Email' and 'Eamil' must be the same.";document.getElementById('p_invalid').innerHTML = "";document.getElementById('sbmt').innerHTML = "One or more errors were found";}}function v_pass() {if (document.getElementById('pass1').value!=document.getElementById('pass2').value){document.getElementById('e_invalid').innerHTML = "";document.getElementById('p_invalid').innerHTML = "'Confirm Password' and 'Password' must be the same.";document.getElementById('sbmt').innerHTML = "One or more errors were found:<br>";}}function v_email2() {if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form_var.e1.value)){document.getElementById('e_invalid').innerHTML = "Does this work??";document.getElementById('p_invalid').innerHTML = "Does this work??";document.getElementById('sbmt').innerHTML = "Does this work??<br>";}}</script></head><body><center><form onSubmit="return checkEmail(this)" formname="form1" action="http://free.hostultra.com/~clonetrooper/petmemorialgarden/php/register.php" method="post" name="form1" id="form1"><table><tr><td>First N<!-- !-->a<!-- !-->m<!-- !-->e:</td><td><input type="text" name="n1"></tr><tr></td><td>Last N<!-- !-->a<!-- !-->m<!-- !-->e:</td><td><input type="text" name="n2"></tr><tr></td><td>E<!-- !-->m<!-- !-->a<!-- !-->i<!-- !-->l:</td><td><input type="text" name="e1"></tr><tr></td><td>Confirm E<!-- !-->m<!-- !-->a<!-- !-->i<!-- !-->l:</td><td><input type="text" name="e2"></tr><tr></td><td>P<!-- !-->a<!-- !-->s<!-- !-->s<!-- !-->word:</td><td><input type="password" name="pass1"></tr><tr></td><td>Confirm P<!-- !-->a<!-- !-->s<!-- !-->s<!-- !-->word:</td><td><input type="password" name="pass2"></tr><tr><td align="center" colspan="2"><div id="sbmt"><input type="submit" value="Register"></div><br></td></tr></table><div id="e_invalid"></div><div id="p_invalid"></div><br><br><br><br><a onclick="validation(this)">hi</a></form></body></html>

It doesn't work on the second page, but does on the first. I am not sure why it stopped working! Help?

Link to comment
Share on other sites

First, a BBCode tip: [codebox] ...code here... [/codebox] is the same as [code] ...code here... [/code], except it adds scrollbars and makes your post much shorter. I use the "Wrap in code tags" control and add "box" twice. (This doesn't work on some other message boards.)Now, some problems with your JavaScript are:

  • You should use type="text/javascript" rather than language="javascript".
  • The function you call onSubmit doesn't exist; validation is the one you should call.
  • validation will always allow submission, because it never returns false.

Also, you should clean up your table; you close several TDs after their TRs, and one TD is never closed. Indenting your code will reveal all such problems.

Link to comment
Share on other sites

Well, I have my function validation() call v_email2(), which should chnge som of the divs to 'Does this work??' but for some reason, it is not working. I will clean up the pages and edit my first post. But the real problem is that when you click 'hi', it does not work... I don't think it has anything to do with the script tags or the OnSubmit. I got it from http://javascriptsource.com . Like I said up there, it works on the first page, but it stops on a second page.

Link to comment
Share on other sites

My complaint about the script tag is due to deprecation. I think JavaScriptSource.com is in the process of updating their code (for both pages and display), but any code taken from there should have obsolete/deprecated portions updated in the mean time. Pretty soon, things like the language attribute should be totally obsolete and not work in current browsers (or at least I think they will drop legacies like that).I posted my other two complaints because that's the beginning of the code; any code which begins that way is doomed to fail. Note that I was submitting the form; I didn't know your means of testing was to click "hi."But if you click "hi," this (equal to the nullstring inside the anchor's onclick event) is passed as form_var. I'm guessing that's not intended.In addition, these calls all return null:

document.getElementById('pass1')document.getElementById('pass2')document.getElementById('e1')document.getElementById('e2')

And for each of these, you try to read the value property... This will break your script.

Link to comment
Share on other sites

And for each of these, you try to read the value property... This will break your script.
How do I fix that problem? Do I add ID="" to all of them? Well, I did that below...I think I will try to fix that later and deal with the email vadiation thing after the other validations are done... here is all the pages scripts again:
<html><head><title>Redister at ***************</title><script LANGUAGE="text/JavaScript">function validation() {v_email();v_p();}function v_email() {if (document.getElementById('e1').value!=document.getElementById('e2').value){document.getElementById('e_invalid').innerHTML = "'Confirm Email' and 'Eamil' must be the same.";document.getElementById('sbmt').innerHTML = "One or more errors were found";}elesif (document.getElementById('e1').value==document.getElementById('e2').value){document.getElementById('e_invalid').innerHTML = "";}}function v_p() {if (document.getElementById('p1').value!=document.getElementById('p2').value){document.getElementById('p_invalid').innerHTML = "'Confirm Email' and 'Eamil' must be the same.";document.getElementById('sbmt').innerHTML = "One or more errors were found";}elesif (document.getElementById('p1').value==document.getElementById('p2').value){document.getElementById('p_invalid').innerHTML = "";}}</script></head><body><center><form action="http://free.hostultra.com/~clonetrooper/petmemorialgarden/php/register.php" method="post" name="form1" id="form1"><table><tr><td>First N<!-- !-->a<!-- !-->m<!-- !-->e:</td><td><input type="text" id="n1" name="n1"></tr><tr></td><td>Last N<!-- !-->a<!-- !-->m<!-- !-->e:</td><td><input type="text" id="n2" name="n2"></tr><tr></td><td>E<!-- !-->m<!-- !-->a<!-- !-->i<!-- !-->l:</td><td><input type="text" id="e1" name="e1"></tr><tr></td><td>Confirm E<!-- !-->m<!-- !-->a<!-- !-->i<!-- !-->l:</td><td><input type="text" id="e2" name="e2"></tr><tr></td><td>P<!-- !-->a<!-- !-->s<!-- !-->s<!-- !-->word:</td><td><input type="text" id="p1" name="p1"></tr><tr></td><td>Confirm P<!-- !-->a<!-- !-->s<!-- !-->s<!-- !-->word:</td><td><input type="text" id="p2" name="p2"></tr><tr><td align="center" colspan="2"><div id="sbmt"><input type="submit" value="Register"></div><br></td></tr></table><div id="e_invalid"></div><div id="p_invalid"></div><br><br><br><br><a onclick="validation()">hi</a></form></body></html>

I revised it a little... but now the Password validation has stoped working. I am sure it has something to do with the Eles stuff... I am still working with if's and are not very good at them.(Should I start a new topic for this problem? :) )

Link to comment
Share on other sites

Uh... else is spelled else, not eles. As in "spell right, or else you will get syntax errors!"Also, it is safer to put to else if on one line like

else if (...)

(Should I start a new topic for this problem?)
No need, it is still moderately relevant (i.e. the overall validation script still isn't working :) )
Link to comment
Share on other sites

Well I am trying to have it do something like below:

if (it is invalid, do below...){show invalid message.}else{if (it is valid, do below...){show valid message.}}

Is there a way to have to if's in a function? Because, in the long run, these things will run in to each other if I try to make them run antoher funtion, because both if's would be trying to replace the innerHTML of one object, and one would overwrite the other... so is there a way to put an if inside an else or put two if's in one function? Last time I attempted to put two if's in a function, it didn't work.

Link to comment
Share on other sites

CloneTrooper, that code should work perfectly, there are no limits to control structures. It must have been something else that was stopping your code from working.

Link to comment
Share on other sites

I think that the else misspelling is ruining the remaining of the code... let me check real quick...1 min later...I guesse the problem was the misspelling! it works! now I have to make email validation...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...