$ten_dang_nhap = 'nguyenvana'; 
$bieu_thuc = '/^[a-zA-Z0-9_]{5,30}$/'; 

if(!preg_match($bieu_thuc, $ten_dang_nhap)){ 

      echo "Ten dang nhap khong hop le"; 

}else{ 

       echo "Ten dang nhap OK";

 }