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++;
}

Recommended Answers

All 2 Replies

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.";
	
	}

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.