How to prevent user registration with Login initiated by number or symbol

Asked

Viewed 199 times

4

I would like the help of you to do the following I have in case this function does in case the login verification to be registered of the user.

If it starts with number send a warning preventing registration. If it did not eat by number it releases the registration of this login.

Here is my problem precisely that this function inhibits the registration of logins initiated by any symbol or character that is not an alphabet letter like @#$% ".+§= */8 among others or started by numbers. Below the function I made.

<?php if(is_numeric(substr($loginPost,0,1))){ ?>
         o Login não pode começar com numero
  <?} else { ?>
          Usuário Cadastrado
   <? } ?>
  • 1

    Checks if it is a-z or A-Z; if it is not, reject login.

2 answers

8

You can perform this validation in several ways, one of them is a regular expression that combines a range of caractatres (a-z). i means that the combination is made by not differentiating capital from minuscule. The function that does research with regex is preg_match()

<?php  
$logins = array('Admin', '007_bond', 'adm', '@dmin');

$regex = '/^[A-Z]/i';

foreach($logins as $item){
    if(preg_match($regex, $item)){
        echo 'login: '. $item .' valido <br>';
    }else{
        echo 'login: '. $item .' invalido <br>';
    }

}   

The other way is to use the function ctype_alpha in the first character of the string that in the example is $item[0].

<?php
$logins = array('Admin', '007_bond', 'adm', '@dmin');

foreach($logins as $item){
    if(ctype_alpha($item[0])){
        echo 'login: '. $item .' valido <br>';
    }else{
        echo 'login: '. $item .' invalido <br>';
    }

}

Example

  • Thank you very much for your help.

  • 1

    This code you can play in a function if you will use in different places.

  • 2

    @Rodrigofp, if the answer agrees, mark it as the accepted answer.

  • I liked the ctype_alpha. In this case, I find it more intelligent than regular expression. + 1

6

I adapted your own logic:

<?php
   $ascii = ord( strtoupper( $loginPost ) );

   if( $ascii < 65 || $ascii > 90 ) {
      echo 'o Login precisa comecar com uma letra';
   else
      echo 'Usuario Cadastrado';
   }
?>


Explaining:

this function returns to string converted to uppercase letters:

strtoupper ( $loginPost );

And this returns the ASCII code of the first character of string:

$ascii = ord( $string );

Then just compare if it’s smaller than ord( 'A' ) (65) or more than ord( 'Z' ) (90):

if( $ascii < 65 || $ascii > 90 ) { ...

... and present the desired message.


More traditional alternative

The top code is equivalent to this, but I wanted to start by presenting less common functions ;)

<?php
   $letra = strtoupper( $loginPost[0] );

   if( $letra < 'A' || $letra > 'Z' ) {
      echo 'o Login precisa comecar com uma letra';
   else
      echo 'Usuario Cadastrado';
   }
?>


Addendum:

$loginPost[0] and substr( $loginPost, 0, 1) are the same thing in this case. Just be careful when using to strings multibyte, because there is no escape from something like this:

$letraMultiByte = mb_substr( $loginPost, 0, 1, 'utf-8' );
  • Thanks even for the help I will also be using both scripts thanks so much for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.