How to prohibit special characters in the user registry?

1

I'm a rookie in this and I do not have much idea, the code is like that

<?php echo $this->Form->input('usuario', array('label' => 'El nombre de tu Equipo','class' => 'form-control', 'placeholder' => 'Por ejemplo: Pepe F.C')); ?>
    </div><!-- .form-group -->
    <div class="form-group">
        <?php echo $this->Form->input('password', array('class' => 'form-control')); ?>
    </div><!-- .form-group -->
    <div class="form-group">
        <?php echo $this->Form->input('confirmar_password', array('type'=>'password','class' => 'form-control')); ?>
    </div><!-- .form-group -->
    <div class="form-group">
        <?php echo $this->Form->input('fecha_nacimiento', array('class' => 'form-control fecha', 'type' => 'text')); ?>
    </div><!-- .form-group -->
    <div class="form-group">
        <?php echo $this->Form->input('email', array('class' => 'form-control', 'placeholder' => '[email protected]')); ?>
    </div><!-- .form-group -->
    <div class="form-group" style="float:right;">
        <?php echo $this->Form->submit('Registrarse', array('class' => 'btn btn-large btn-primary')); ?>
    </div><!-- .form-group -->
    </fieldset>

<?php echo $this->Form->end(); ?>

Thank you very much

    
asked by mrcroto 06.02.2018 в 22:04
source

3 answers

0

You can use preg_replace to remove special characters from the entry, like this:

preg_replace('/[^A-Za-z0-9\-]/', '', input('usuario', array('label' => 'El nombre de tu Equipo','class' => 'form-control', 'placeholder' => 'Por ejemplo: Pepe F.C'))

You can also use preg_match to verify if an entry has a special character, and then handle it as you wish, like this: if(!preg_match('/[^a-zA-Z\d]/', $string)){//enviar}

    
answered by 06.02.2018 в 22:15
0

Instead of prohibiting special characters (which are many) you can try delimiting only those that do accept, for example, digits and letters in upper and lower case, for example:

$regexp = '/^[a-z0-9]+$/i';
$entrada = 'user_name';

$resultado = preg_match($regexp, $entrada);
if(!$resultado) {
  echo 'entrada incorrecta: ' . $entrada . PHP_EOL;
}

$entrada = 'userName';
$resultado = preg_match($regexp, $entrada);
if($resultado) {
  echo 'entrada correcta: ' . $entrada . PHP_EOL;
}
    
answered by 06.02.2018 в 23:05
0

Remember that PHP is a language that runs on the server, once the user wants to fill out the fields PHP will no longer have jurisdiction over what happens, until the form is sent once again to the server. In the browser what you can do to avoid entering characters that you do not allow is to add a JavaScript event which has jurisdiction over what happens while the user interacts with your page

$("#usuario").on('keypress', function (event) {
        let caracteresPermitidos = /[a-z0-9]/i;
        let code = event.which;
        let keyPress = String.fromCharCode(code);
        if (keyPress.search(code) < 0) {
            event.preventDefault();
            event.stopImmediatePropagation();
            return;
        }
    });

This will cause you to enter only characters that are letters (uppercase or lowercase) or numbers. But in addition you must do the validation on the server side as indicated by the other partners

    
answered by 07.02.2018 в 00:52