|
|
|
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 | |
|
|
|