Reader
  • Home
  • About
  • Deals
  • Services
  • Contact

Validate (input) username

Last updated on March 31st, 2009 by Gabriel Livan

Hello coders,

Here’s a function that validates a username. It can be used when you have a register form and the user should enter a valid username (that shouldn’t contain illegal characters).

<?php
function validate_username($username)
{
$check = eregi_replace('([a-zA-Z0-9_]+)', "", $username);

if(empty($check))
	{
	return true;
	}
	else
	{
	return false;
	}
}

$username = 'john_jones';

$validator = validate_username($username);

if($validator)
{
echo 'The username is valid.';
}
else
{
echo 'The username should contain only letters, numbers and underscores.';
}
?>

If you use ‘john#_$%jones’ as an username the function will return ‘false’.

If you have any comments / suggestions feel free to post them.

Filed Under: PHP Tagged With: validate

About Me

Gabriel LivanHey, there! I'm Gabriel Livan, Web Developer, Blogger and Internet Marketer. I love making fast, secure and scalable web applications, running and travelling around the world. I blog about WordPress, site speed & security and quality tools for web professionals. Read more

What are you looking for?

Categories

  • AJAX (22)
  • Deals (10)
  • Design (30)
  • eBooks (2)
  • Flash (19)
  • Freebies (11)
  • General (62)
  • Giveaways (1)
  • Graphics (27)
  • HTML & CSS (35)
    • Frameworks (9)
  • JavaScript (160)
    • Frameworks (8)
    • jQuery (89)
    • MooTools (14)
  • Page Loading Speed (2)
  • Photoshop (3)
  • PHP (113)
  • Reviews (8)
  • SEO (6)
  • Statistics (2)
  • Web Tools (13)
  • WordPress (27)
    • Plugins (15)
    • Snippets (4)
    • Tools (2)
    • Tutorials (6)

Copyright © 2026 · BitRepository.com - All Rights Reserved

  • Home
  • About
  • Contact