How to demand a specific amount of characters in a field? [closed]

0

I want a certain field in specific can only have 16 characters, no more, no less, I'm using php.

    
asked by Hoozuki 12.06.2018 в 23:04
source

3 answers

0
$ciertoCampo = $_POST['cierto-campo']; 
// Supongo que al ser un dato de 16 caracteres necesitarás POST
if (strlen($ciertoCampo) == 16) {
      // Lo que tengas que hacer con los datos recibidos
} else {
      // Algún mensaje indicando que son necesarios 16 caracteres exactamente
}
    
answered by 13.06.2018 / 20:06
source
1

Validations start on the client side and end on the server side. The first for the comfort of the user and the second for security (never trust the client side).

Customer

If we assume that your client is html + css + js the simplest alternative are the minlength and maxlength input attributes (provided that it is text, email, search, password, tel, or url):

<iput name="mi-input" id="mi-input" type="text" minlength="16" maxlength="16">

The advantage of this is that if the input is sent using the submit event of the form it belongs to (and if the browser is relatively modern) the form will not be sent and a tooltip will be displayed in the input

and on the Server

$miInput = $_REQUEST["mi-input"]; /* cambiar $_REQUEST por el metodo apropiado ($_GET o $_POST) */
if( strlen($miInput) != 16 ){
 /* actuar en consecuencia */
}

Other customer alternatives

Illustratively you could also add a listener to the submit event (if the field is in a form) and check the size before sending:

<form method="POST" action="" id="mi-formulario">
 <input name="mi-input" id="mi-input" type="text" minlength="16" maxlength="16">
 <button type="submit">OK</button>
</form>
 <script>     
   document.getElementById("mi-formulario").addEventListener("submit", function(e){
       e.preventDefault();
       var miInput = document.getElementById("mi-input").value;
       if( miInput.length != 16 ){
           /*
           actuar en consecuencia, mostrar un mensaje u otra acción
           */
       }
    })
 </script>

If for some reason the form is not sent using a submit event, a simple alternative is to add a listener to the button or to the element that initiates the submission of the form:

    <form method="POST" action="" id="mi-formulario">
     <input name="mi-input" id="mi-input" type="text" minlength="16" maxlength="16">
     <button type="button" id="mi-boton">OK</button>
   </form>
   <script>     
     document.getElementById("mi-boton").addEventListener("click", function(e){
       e.preventDefault();
       var miInput = document.getElementById("mi-input").value;
       if( miInput.length != 16 ){
           /*
           actuar en consecuencia, mostrar un mensaje u otra acción
           */
       }
    })
 </script>

Always remember to validate your forms on the server side, on the client side it is relatively easy to alter the code to enter more characters in the field.

some links of interest:

answered by 13.06.2018 в 00:09
0

You could do it on the client side with JavaScript

if(campo.length == 16){
 /*Acción que se vaya a realizar cuando sea valido*/ 
}else{
  alert("El campo debe de tener 16 caracteres.");
}
    
answered by 12.06.2018 в 23:08