Hi, I would like it to give me an error message if the input string length is less than three characters. Where am I wrong? I also have checkboxes inside the function that I removed to simplify the code.
<pre lang="HTML"><pre><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Form Car Parking</title> <link rel="stylesheet" href="./css/bootstrap.min.css"> <script src="./js/jquery-3.6.0.min.js"></script> <script src="./js/bootstrap.bundle.min.js"></script> </head> <body> <form action="FormCarParking.php" method="POST"> <div class="section"> <div class="row"> <div class="col-md"> <div class="form-group"> <label for="fname">Nome</label> <input class="form-control" type="text" id="utente" name="fname" placeholder="Inserisci il nome" required> </div> </div> <!-- <div class="col-md"> <div class="form-group"> <label for="fname">Nome</label> <input class="form-control" type="text" name="lname" placeholder="Inserisci il cognome" required> </div> </div>--> </div> <input type="submit" class="btn btn-primary" value="Invia"> </form> </body> <script> (function() { const selectionCheckbos = document.querySelector('.section'); const text = document.querySelectorAll('input[type=text]'); const textLength = text.length; const firstText = textLength > 0 ? text[0] : ''; function initText() { if (firstText) { for (let i = 0; i < textLength; i++) { text[i].addEventListener('change', textValidity); } textValidity(); } } function chekText() { const min = 3; if (firstText.length >= min) { return true; } return false; } function textValidity() { const errorMessage = !chekText() ? 'Il campo non valido' : ''; firstText.setCustomValidity(errorMessage); } initText(); })(); </script> </html>
text
firstText
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)