Reader
  • Home
  • About
  • Deals
  • Services
  • Contact

Validate (input) password

Last updated on July 7th, 2009 by Gabriel Livan

This is a useful function if you need to validate a password (input). If you have a form and you need to check if the username, which is registering, entered a valid password (without illegal characters) then this would help you to do it. Only letters, numbers and underscores are accepted.

<?php
/*
Credits: http://www.bitrepository.com/
*/

// default chars values are defined
function validate_password($password, $min_char = 4, $max_char = 20)
{
// Remove whitespaces from the beginning and end of a string
$password = trim($password); 

// Accept only letters, numbers and underscore
$eregi = eregi_replace('([a-zA-Z0-9_]{'.$min_char.','.$max_char.'})', 
'', $password);

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

$password = 'my_password_123';

/*
First parameter: Password
Second parameter: Minimum chars 
Third parameter: Maximum chars
*/

$validator = validate_password($password, 4, 20);

if($validator) 
{
// Our current example will return true
echo 'The password is valid.';
}
else 
{
/* if our password value is for instance: my_@#%_password 
our function will return false */
echo 'The password appears to be invalid. 
It should contain only letters, numbers and underscore.';
}
?>

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