Posted By

robertdotim on 02/11/11


Tagged

php numbers validate letters alphanumeric


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

robertdotim


Validate an alphanumeric string


 / Published in: PHP
 

URL: http://robert.im

This function uses the preg_replace counter to check for any non-alphanumeric characters.

You can use this for username / password validation if you don't want any special characters to be used.

  1. <?php
  2.  
  3. function validate_alphanum($string)
  4. {
  5. $count = 0;
  6. preg_replace("/[^a-zA-Z0-9]/", "", $string, -1, $count);
  7.  
  8. if($count != 0)
  9. {
  10. return false;
  11. }
  12.  
  13. return true;
  14. }
  15.  
  16. ?>

Report this snippet  

You need to login to post a comment.