fork download
  1. <?php
  2.  
  3. function validateAlphAtleastTwoChars($val) {
  4. if(!preg_match('/^[a-zA-Z]{2,}$/', $val))
  5. return false;
  6. return true;
  7. }
  8.  
  9. $names = [
  10. '123',
  11. 'James',
  12. 'Artie2',
  13. '<script>alert("Hello);</script>',
  14. ' ',
  15. 'Li',
  16. 'I'
  17. ];
  18.  
  19. foreach( $names as $name){
  20. echo "{$name} is only alphanumeric and at least 2 characters " . json_encode(validateAlphAtleastTwoChars($name));
  21. echo "\n";
  22. }
  23.  
  24.  
  25.  
Success #stdin #stdout 0.02s 23752KB
stdin
Standard input is empty
stdout
123 is only alphanumeric and at least 2 characters false
James is only alphanumeric and at least 2 characters true
Artie2 is only alphanumeric and at least 2 characters false
<script>alert("Hello);</script> is only alphanumeric and at least 2 characters false
    is only alphanumeric and at least 2 characters false
Li is only alphanumeric and at least 2 characters true
I is only alphanumeric and at least 2 characters false