WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Username Validation function
Categories : PHP, Beginner Guides, Regexps Click here to Update Your Picture
Gerhard Gerome
Date : Mar 16th 2007
Grade : 3 of 5 (graded 2 times)
Viewed : 2977
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Gerhard Gerome
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This function can be used to validate usernames (or other strings). The maximum and minimum length of the username can be set. Also you are allowed to set the allowed characters.


<html>
<head>
<title>Username Validity</title>
</head>
<body>
<?php

// During user registration we may want to limit the length and characters used in username
// This function can help you do that. It returns true if valid else false.

function is_valid_username($username){
   
// Enter other valid characters below
   
static $valid_chars = "._+?";
    static
$min_length = 5;
    static
$max_length = 15;
    if(!
eregi("^[a-z0-9$valid_chars]{".$min_length.",".$max_length."}$",$username)){
        return
false;
    } 
    return
true;
}
?>
</body>
</html>



Usage Example
<?php
// Call the function is_valid_username with the username as the argument
$username = "gerhard";
if(
is_valid_username($username)){
    echo
"$username is valid";
} else {
    echo
"$username is invalid";
}
?>



a PHP Function to Get only the filename (remove the extension) using regular expressions.
Categories : PHP, Regexps, Beginner Guides
Newbie Notes #7 - Ridiculous regex
Categories : PHP, Beginner Guides, Regexps
Validating a URL with preg_match
Categories : PHP, Regexps, Beginner Guides, Data Validation
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Find the day of the week for any given year/month/day.
Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides
Upload Via FTP - an alternative to move_uploaded_file
Categories : PHP, FTP, Beginner Guides
Calculate Body Mass Index
Categories : PHP, Algorithms, Regexps
How to strip non-alpha characters from a string
Categories : Regexps, PHP
PHP and javascript mouseover, mouseout, and mousedown events
Categories : PHP, Java Script, Form Processing, Beginner Guides
How to preset a text string in a textarea input field
Categories : HTML, HTML and PHP, PHP, Beginner Guides
How to check if a string has only specific characters?
Categories : PHP, Regexps
Easy alert box pop-up function
Categories : PHP, Java Script, Beginner Guides
Newbie Notes #1 - Making a form return to itself
Categories : PHP, Beginner Guides, HTML and PHP
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
mySQL/PHP/search with multientry form and table output with colored rows
Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases