Jump to content

Username Validation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Hey guy's

I need a piece of code that validates a username so it's only allowed lower case letters, numbers, underscores and dashes.

No spaces, dots ect.

Thank's!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You could use a regular expression: ^[a-z0-9_-]*$
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Yep, WingedPanther is right. If you're not sure how to use the regular expression, here's an example using preg_match :

<?php
$username = "some_username"; //e.g. $_POST['username'] 
 
if(preg_match("/^[a-z0-9_-]*$/", $username)) {
 
   //Valid username
 
}else{
 
   //Invalid username
 
}

?>