Tengo un problema chicos. Como principiante, intento verificar la longitud de la contraseña y devolver el resultado, pero no funciona. después de hacer clic en el botón, no hace nada. ¿Qué estoy haciendo mal?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <script> function checkpassword(){ var pas=document.getElementByName(passcode).value; var x=pas.length; if(x<8){ document.getElementById(message).innerHTML="Error 404!"; } else{ document.getElementById(message).innerHTML="It's acceptable" } } </script> <body> <input type="password" name="passcode" placeholder="Enter password to check"> <input type="submit" value="submit" onclick="checkpassword()"> <p id="message"></p> </body> </html>@'Felix Kling' y @'Daniel A. White han dado algunas pistas en los comentarios porque tendrá que pasar los nombres y las identificaciones como cadenas. Pero otro problema es que .getElementByName() no es una función. Puede probar esto (tenga en cuenta que he usado .getElementsByName (plural) y he hecho referencia al índice cero-ésimo:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <script> function checkpassword(){ var pas=document.getElementsByName('passcode')[0].value; var x=pas.length; if(x<8){ document.getElementById("message").innerHTML="Error 404!"; } else{ document.getElementById("message").innerHTML="It's acceptable" } } </script> <body> <input type="password" name="passcode" placeholder="Enter password to check"> <input type="submit" value="submit" onclick="checkpassword()"> <p id="message"></p> </body> </html>