Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript form validation

Status
Not open for further replies.

MikeM2468

IS-IT--Management
Apr 5, 2011
100
US
I'm looking to validate a PHP form before it gets submitted. I've seen this and it looks promising, but I need more options.

Here is the PHP code that I'm using to validate an already submitted form. I'd like to transform it to javascript so it can be validated before submission. Since I'm implementing sessions, the existing javascript back link doesn't work.

Code:
$badpassword = array("password", "123", "abc", "xyz", "qwerty", "asdf", $login, $firstname, $lastname);
for($x = 0; $x < count($badpassword); $x++) {
	preg_match("/".$badpassword[$x]."/i", $pswrd, $matches);
	if(count($matches) > 0) {
		echo "Password contains the prohibited combination '$badpassword[$x]'.";
		print ('<br />');
		echo "Hit the back button to try again."; 	
		print ('<br />');
		exit ();
	}
}
if( !preg_match("#[0-9]+#", $pswrd) ) { 
	echo "Password must include a number!"; 
	print ('<br />');
	print ('<a HREF="javascript:history.back()">Click here to try again.</a> ');
	print ('<br />');
	exit();
}
if( !preg_match("#[a-z]+#", $pswrd) ) { 
	echo "Password must include a letter!"; 
	print ('<br />');
	print ('<a HREF="javascript:history.back()">Click here to try again.</a> ');
	print ('<br />');
	exit();
}
if( !preg_match("#\W+#", $pswrd) ) {
	echo "Password must include a symbol!"; 
	print ('<br />');
	print ('<a HREF="javascript:history.back()">Click here to try again.</a> ');
	print ('<br />');
	exit();
}
 
Without seeing your HTML its hard to be specific, but in general, you'll have to run your validation function at the onsubmit event of the form so you canstop it if there is an error in the form, for instance:

Code:
[b][COLOR=#0000FF]<form[/color][/b] [COLOR=#009900]action[/color][COLOR=#990000]=[/color][COLOR=#FF0000]"process.php"[/color] [COLOR=#009900]method[/color][COLOR=#990000]=[/color][COLOR=#FF0000]"..."[/color] [COLOR=#009900]onsubmit[/color][COLOR=#990000]=[/color][COLOR=#FF0000]"return ValidateForm();"[/color][b][COLOR=#0000FF]>[/color][/b]

Code:
[COLOR=#990000]<[/color]script type[COLOR=#990000]=[/color][COLOR=#FF0000]"text/javascript"[/color] [COLOR=#990000]>[/color]
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]ValidateForm[/color][/b][COLOR=#990000]()[/color]
[COLOR=#FF0000]{[/color]
[tab][i][COLOR=#9A1900]//Get password Field Object reference[/color][/i]
[tab][b][COLOR=#0000FF]var[/color][/b] passfield [COLOR=#990000]=[/color] document[COLOR=#990000].[/color][b][COLOR=#000000]getElementById[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'thePass'[/color][COLOR=#990000]).[/color]value[COLOR=#990000];[/color]
[tab][i][COLOR=#9A1900]//Define array of prohibited passwords[/color][/i]
[tab][b][COLOR=#0000FF]var[/color][/b] badpassword [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b] [b][COLOR=#000000]Array[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"password"[/color][COLOR=#990000],[/color] [COLOR=#FF0000]"123"[/color][COLOR=#990000],[/color] [COLOR=#FF0000]"abc"[/color][COLOR=#990000],[/color] [COLOR=#FF0000]"xyz"[/color][COLOR=#990000],[/color] [COLOR=#FF0000]"qwerty"[/color][COLOR=#990000],[/color] [COLOR=#FF0000]"asdf"[/color][COLOR=#990000]);[/color]
[tab][i][COLOR=#9A1900]//Cycle through array of prohibited password strings and see if they exist in provided password [/color][/i]
[tab][b][COLOR=#0000FF]for[/color][/b][COLOR=#990000]([/color][b][COLOR=#0000FF]var[/color][/b] i[COLOR=#990000]=[/color][COLOR=#993399]0[/color][COLOR=#990000];[/color] i[COLOR=#990000]<=[/color]badpassword[COLOR=#990000].[/color]length[COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000];[/color] i[COLOR=#990000]++)[/color]
[tab][COLOR=#FF0000]{[/color]

[tab][tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color]passfield[COLOR=#990000].[/color][b][COLOR=#000000]indexOf[/color][/b][COLOR=#990000]([/color]badpassword[COLOR=#990000][[/color]i[COLOR=#990000]])!=-[/color][COLOR=#993399]1[/color][COLOR=#990000])[/color]
[tab][tab][COLOR=#FF0000]{[/color]
[tab][tab][tab][b][COLOR=#000000]alert[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"Bad String: "[/color] [COLOR=#990000]+[/color] badpassword[COLOR=#990000][[/color]i[COLOR=#990000]]);[/color]
[tab][tab][tab][i][COLOR=#9A1900]//return false if prohibtied string found in passowrd[/color][/i]
[tab][tab][tab][b][COLOR=#0000FF]return[/color][/b] [b][COLOR=#0000FF]false[/color][/b][COLOR=#990000];[/color]   
[tab][tab][COLOR=#FF0000]}[/color]
[tab][COLOR=#FF0000]}[/color] 
[tab][i][COLOR=#9A1900]//return true of no prohibited strings are found.[/color][/i]
[b][COLOR=#0000FF]return[/color][/b] [b][COLOR=#0000FF]true[/color][/b][COLOR=#990000];[/color]
[COLOR=#FF0000]}[/color]
[COLOR=#990000]</[/color]script[COLOR=#990000]>[/color]

Obviously the Id for the field object references will need to be changed to match your fields in your form.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
This looks promising but I realized that it won't work. I have post submission checks that evaluate whether the user is required to have certain password restrictions. I've altered the scripts to use $_GET so navigation will work.
 
Not sure how using GET is going to fix navigation. But if it works for you that's fine.

If there are any word restrictions for a user password you need to send over to the JS validation, you can simply echo them out in a valid Js array format.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top