Jump to content

I have a register page, and need a script to check everything.


clonetrooper9494

Recommended Posts

I have a register page, and need a script to check everything. It needs to make sure that there are no spaces in the name box, make sure that it is a valid email or contains a @, and that the Password box and Confirm Password box are the same. is there any pre-made script some where to do that, or do I need to look up all this stuff up and start from scratch?

Link to comment
Share on other sites

Sounds a lot like my final project from my high school HTML & JS class. I won't share that code outright with you, but I will point you in the direction you need to follow. You'd have to model something like this by yourself though.Basically, you will be wanting to check if there are certain values present within the forms. I find the match() function of JS is the best for the job. You can use it in a conditional statement to check for something real easy like and whatnot.

/*Checks for string presence within another string */if(string_1.match("some string value here")) {/*Do Code - Report Error */}

or

/*Check for string absence within another string */if(!string_1.match("some string value here")) {/*Do Code - Report Error */}

Of course, the string you would be testing is would be something like document.form_1.value. For valid email though, you'd have to make sure there is at least 1 "." and a three nonnumerical characters would have to follow. You could try something like finding all the values after the last ".", put them into a substring, and check if the length of that is equal to the appropiate amount. I'd use the function lastindexOf() and substr() for that.

Link to comment
Share on other sites

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 interpret it into the register page... but that didn't work. Did I do something wrong?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!! [url="http://javascript.internet.com"]http://javascript.internet.com[/url] --><!-- Beginfunction checkEmail(myForm) {if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('e_invalid').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

Just remeber that you still need to do the validation on the server side too. Someone can disable JS and bypass your validation. Doing it client side first just saves trips to the server but validation should be done always on the server as well.

Link to comment
Share on other sites

When you say it should be on the sever too, do mean I should try using PHP, or can that be disabled to? I have no accsess to a SQL, or any other fnancy scripts other than PHP. I am on a free hosting web site which doesn't allow SQL or ASP pages (but still, infinite space/bandwidth, great deal!). so should I try and have PHP validate it too?

Link to comment
Share on other sites

Yeah you should still use PHP. Javascript validation is convenient for the user because they don't have to wait for the page to reload to see what happened, but using Javascript doesn't guarantee that the data is valid, the user could have turned Javascript off. With PHP there's nothing to disable.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...