Validate an alphanumeric string


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. ?>

URL: http://robert.im

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.