Validation: A-z 0-9 only

I am trying to validate a textbox so the user can only submit an alphanumeric string, no special characters or symbols.

Here is what I have from following a tutorial on validating, changing it to match my needs, but it is giving me an error.

[php]if(preg_match($username) “/^[0-9a-zA-Z_]$/”) $errors[] = ‘• Username must be characters or numbers only’;[/php]

The error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/a4566300/public_html/yolosignup.php on line 21

Thanks for your help

Hi Penny94,

I would suggest a slight change to your regex. I left out the underscore after the Z, if you do want to allow underscores, you will need to add it back in. You also need to implement preg_match differently.

See if this works for you:[php]preg_match(’/^[0-9a-zA-Z]+$/’ , $username, $match);
if($username !== $match[0]) $errors[] = ‘• Username must be characters or numbers only’;[/php]

Let me know if this doesn’t work for you.

jay

One thing is the order.

[php]<?php if(preg_match('/[^0-9A-Za-z]/',$username)) // this is the right format i beleive ?>[/php]

Thanks, I got it working now.

However I have a new problem…

I am trying to make sure the passwords are the same:

[php]if(!$password == $repassword) $errors[] = ‘• Passwords do not match’;[/php]

I thought if(!condition) meant ‘if not’…?

Thanks

This should work

[php]if($password != $repassword) { echo “Password doesnt match.”; }
else {
echo “Password matches”;
}[/php]

How would I include that within my code?

[php]if (isset($_POST[‘username’], $_POST[‘realname’], $_POST[‘password’], $_POST[‘repassword’], $_POST[‘email’]))
{
$errors = array();

$username = $_POST['username'];
$realname = $_POST['realname'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$email = $_POST['email'];

if (empty($username) || empty($realname) || empty($password) || empty($repassword)|| empty($email)) 
  {
    $errors[] = 'All fields are required';
  }
else
  {
    if(strlen($username) > 25 ) $errors[] = '&#8226; Username can not be longer than 12 characters';
    if(strlen($username) < 3 ) $errors[] = '&#8226; Username must be 3 characters or more';

if(preg_match(’/[^0-9A-Za-z]/’,$username)) $errors[] = ‘•asdfadfafadfadfadsf’;
if(strlen($realname) < 2 ) $errors[] = ‘• Real name can not be shorter than 2 characters’;
if(strlen($password) < 5 ) $errors[] = ‘• Password must be 5 characters or more’;
if(!$password == $repassword) $errors[] = ‘• Passwords do not match’;
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) $errors[] = ‘• Please enter a valid email address’;
}

if(!empty($errors))
  {
    foreach($errors as $error)[/php]

I think you need to put the folowing line:
[php]if($password != $repassword) { $errors[] = ‘• Passwords do not match’; }[/php]
in place of your:
[php]if(!$password == $repassword) $errors[] = ‘&#8226; Passwords do not match’;[/php]
also your foreach variables are pretty similar could be confusing if you have to go back and look at your code. You didnt post the rest of the foreach code which needs to be correct for your errors to display. i just ran this code real quick to make sure everything works. hope this helps!
[php]<?php
$password=“password”;
$repassword=“mispelled_password”;
$errors = array();
if($password != $repassword) { $errors[] = ‘• Passwords do not match’; }

if(!empty($errors))
  {
    foreach($errors as $errorreport)
	{
	echo $errorreport;
	}
	}

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service