Validation of PHP fields

2

I already have my index in which I log in with username and password, the question is that when giving blank space in both fields allows entering the menu, some sentence in script or php to validate and prevent this happen?

form code:

<center><form name='login' class="register" method='post' action='login.php'>

              <tr>
                 <td><label>Usuario:</td>
              </tr>
              <tr>
                 <td><input type='text'  class="register-input" required name='Usuario' maxlenght='10'></label></td>
              </tr>
              <tr>
                 <td><label>Clave/RP:</td>
              </tr>
              <tr>
                 <td><input type='password' class="register-input" required name='RPE'></label><br><br></td>
              </tr>
              <tr>
                 <td><center><input type='submit' class="register-button" id='boton' value='Iniciar sesión'></label></center></td>
              </tr> 

  </form></center>

login.php code

   <div class="menu" id=registro>
   <?php session_start();

    $Usuario=$_POST['Usuario'];
    $RPE=$_POST['RPE'];
    $query = "SELECT idAdmin, Usuario FROM administradores WHERE 
     Usuario='$Usuario' AND    RPE='$RPE'";

    include('libreria.php');
    $result=conectar_bd($query);
        if ($row = mysqli_fetch_array($result))
        {

           $_SESSION['idAdmin']=$row[0];
           $_SESSION['Usuario']=$row[1];
             header ("Location: menu.php");

         }
         else
         {
           echo "<center>ERROR: No existe el usuario o la clave es incorrecta. <br><br></center>";
           echo "<center><a href='index.php'>INTENTAR DE NUEVO</a><br> 
     </center>";
           }       
     ?>     
     <center><img src="images/error.png" width="350" height="250" alt="Logo 
CFE"/</center>
  </div>
    
asked by Jair Mejía López 19.04.2018 в 17:40
source

2 answers

1

A simple option for your solution would be to add a pattern to filter the first space in your inputs. As follows:

<input type="text" class="" name="" maxlenght="10" pattern="[^ ].*" required>

And the same for the input of the password. What we do here is tell you that the first character can be anyone except a blank space.

Also, if you also want to avoid other types of spaces like tabs you can use the following: pattern="\S.*" .

    
answered by 19.04.2018 / 18:19
source
4

You can in the variables that contain the value of your inputs, of the TRIM method that helps to eliminate blank spaces at the beginning and end

Without Trim ()

<?php

$cadenaUno = "    Alfa";
$cadenaDos = "Alfa";

if($cadenaUno === $cadenaDos){
    echo "son iguales";
}else{
    echo "No son iguales";
}
  

As notes both chains although they have the same text, they are not the same   for the spaces at the beginning and returns as a message

     
    

"They are not the same"

  

With the Trim () function

<?php

$cadenaUno = trim("    Alfa");
$cadenaDos = "Alfa";

if($cadenaUno === $cadenaDos){
    echo "son iguales";
}else{
    echo "No son iguales";
}
  

In the previous example, although the first string has spaces at   start, thanks to trim () removes them and sends the message that both   They are the same

WITH YOUR VARIABLES

<?php
$Usuario=trim($_POST['Usuario']);
$RPE=trim($_POST['RPE']);
    
answered by 19.04.2018 в 17:56