Jump to content

validation of form fields errors


real_illusions
 Share

Recommended Posts

I want to validate some form info, making sure what the user fills out is the right length and only consists of numbers and letters. I found the basic model to work from to validate it, but i want it to echo an error message if its filled out wrong. If its filled out correctly, then it needs to be passed to a variable for processing later on in the script to be written to a database. However this produces errors and i cant work out whats causing them...This at the moment causes this error - Parse error: syntax error, unexpected T_ELSE in form.php on line 14Line 14 is the 'else'// validate form infoif ((strlen($name)<3) ||(strlen($name)>15) ||(!ctype_isalpha($name)))echo "Your name must be between 3 and 15 characters long. And only contain numbers or letters";{else ($name = $_POST["name"];)}What am i doing wrong? (and dont say 'everything' :) :) ):mellow:

Link to comment
Share on other sites

you misplaced the curly bracketsopen the curly brace before first echo and remove the last onelike this

if ((strlen($name)<3) || (strlen($name)>15) || (!ctype_isalpha($name)) ){	echo "Your name must be between 3 and 15 characters long. And only contain numbers or letters";}else(	$name = $_POST["name"];)

"Your name must be between 3 and 15 characters long. And only contain numbers or letters"
ctype_alpha checks if all characters are alphabetic, if you want to allow users to write numbers and letters you should use ctype_alnum
Link to comment
Share on other sites

Thanks..but its still making errorsif (((strlen($name)<3) ||(strlen($name)>15) ||(!ctype_alpha($name))){echo "Your name must be between 3 and 15 characters long. And only contain letters";}else ($name = $_POST["name"];)Such as the { just before the echo...says its a parse error - unexpected { on that line.Isn't there one too many ( in there?:)

Link to comment
Share on other sites

Adding in my two cents, why not just validate on the javascript side? And couldn't preg_match be used instead of all of those checks? If i'm not mistaken, something like

$name = preg_replace('/([0-9]+)/', '',$name);if(preg_match('/[a-z]{3,15}/i',$name)){   echo "Good Name";}

Would do the exact same thing as all 3 of those other checks...

Link to comment
Share on other sites

Jhecht - I want to do this server side as javascript can be turned off :)Its still not working. I type in some test fields, press submit, but yet the error lines appear, despite the fields having the right number of characters, and it also processes the info and sends it.How do you stop the information been sent if the error messages are displayed?:)

Link to comment
Share on other sites

Ok..how would i go about this?I've looked over the error strings and various error functions on php.net but not seem to be getting anywhere with it. All the examples on there seem to be about creating custom error messages to do with developing php scripts and not validating information coming from another page.:)

Link to comment
Share on other sites

I think here:

if ((strlen($name)<3) || (strlen($name)>15) || (!ctype_isalpha($name)) )

it should by this:

if ((strlen($_POST['name'])<3) || (strlen($_POST['name'])>15) || (!ctype_isalpha($_POST['name'])) )

Link to comment
Share on other sites

here's a script, I'll assume you just want to validate the username so that its only letters and is between 3 and 15 characters long.

<?php  $errorString = '';  if(sizeof($_POST)==0){	 $errorString .= 'No form data submitted<br />';	 die($errorString);   }  if(!preg_match('/^([a-z]{3,15}$/',$_POST['user_name'])){	 $errorString .= "Username is not long enough<br />";	}   //You could potentially add more checks before echoing out this string. just put $errorString.= ' Your error Message Here<br />';  if(strlen($errorString)!=0){		die($errorString);  }else{	  //Whatever you need to do now, insert DB and so on.   }?>

Link to comment
Share on other sites

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 14 in <filename> on line 12Then it prints the error message for the username, despite the username been long enough.Line 12 in the file is if(!preg_match('/^([a-z]{3,15}$/',$_POST['user_name'])){If i wanted to validate the 3 fields, do i need to do that 3 times but with the various values changed to email and comments and any field lengths that might be different. And in the last bit, where it says}else{ //Whatever you need to do now, insert DB and so on.Can i put in $name = $_POST["name"];?? Or will that not work?:)

Link to comment
Share on other sites

if(!preg_match('/^([a-z]{3,15})$/',$_POST['user_name'])){Change the line to that, i forgot a parentheses. And yes, you can do the $name = $_POST['name'] thing, but why would you? It'd be just as easy to call $_POST['name'];And if there are 3 fields that must be validated differently, then you could just change the regex. I would need to see the form you're using to give you an appropriate file.

Link to comment
Share on other sites

Ah ok..thx. I couldn't see where the missing bracket was from. It all looked ok to me.Now it seems to work. However, I think it doesn't seem to recognise the space bar character. If i test the form with the right amount of letters without spaces, it works, but add a space (even after the 3rd character) it doesn't work.How do i go about so it recognises the space bar as a character?Can you explain'/^([a-z]{3,15})$/'to me? As thats the bit that defines what characters are allowed right?:)

Link to comment
Share on other sites

Can you explain'/^([a-z]{3,15})$/'to me? As thats the bit that defines what characters are allowed right?:)
That's a regular expression. There are tons of tutorials on the net for it.To expand the above one to include numbers and spaces, try something like:
'/^([a-z0-9\s]{3,15})$/'

Link to comment
Share on other sites

Ok..how would i go about this?I've looked over the error strings and various error functions on php.net but not seem to be getting anywhere with it. All the examples on there seem to be about creating custom error messages to do with developing php scripts and not validating information coming from another page.
$errors = '';if (...)  $errors .= 'error 1';if (...)  $errors .= 'error 2';if (...)  $errors .= 'error 3';if ($errors == ''){  // no errors}else  echo $errors;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...