954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Having trouble with my form validation

I'm having a trouble getting my php script to validate user input what am I doing wrong here? This just displays the error message all the time.

for example:

if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\ ]+$/",$name)||empty($name))
{
               print '<td colspan="2" class="ErrorText"><div align="right">Please enter a valid name without special characters i.e. *,/,&lt; etc. </div></td>';
							$error++;
}
wireshop
Newbie Poster
3 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

I don't use preg_match, but this code works for me to make sure users only use numbers and letters.

if (eregi ("^[[:alnum:]]+$", $_POST['username'])) {
		$a = TRUE;
	} else {
		$a = FALSE;
		$message[] = "Please enter a username that consists only of letters and numbers.";
	
	}
nanharb
Newbie Poster
7 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

You can also use ctype_alnum, this way:

$username = ctype_alnum($_POST['username']) ? $_POST['username'] : NULL;

if($username == NULL) {
  echo 'error message';
} else {
  echo $username;
}
cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You