2

I am currently trying to create a very simple validation script with JS. Basically, I want a alert to come up if the text inputted into a form is shorter than 5 characters, or longer than 25.

     <!doctype html>
     <html>
       <head>
       <title>Password Validator</title>
       </head>

      <body>
        <script>
        function validate()
        {  
        var yourpw = document.forms.passwordform.yourpassword.length; 

        if (yourpw < 5);
        {
        alert("password is too short");   
        return false;
        }

        if (yourpw > 25)
        {
        alert("your password is too long")
        return false;
        }
        }
        </script>


        <h1>Password Validator</h1>
        <p>Please create a new password below</p>
        <p>Be sure you password follows these guidelines</p>
        <ul>
          <li>No shorter than 5 characters</li>
          <li>No longer than 25 characters</li>
        </ul>

         <br>

         <form name="passwordform" method="post" onsubmit="validate();">
         <input type="text" name="yourpassword">
         <br>
         <input type="submit">
         </form>    

       </body>
    </html>   

I am not sure what exactly I am missing or why it wont work but the goal of what i want is that when text is inputted into the text box named "yourpassword", a script will run that will show a message if either one of these conditions are met: shorter than 5 characters, or longer than 25, warning the person typing that their password does not follow the guidelines, if the password meets the guidelines, then i just want a simple confirmation message to appear. Anyways i appreciate any help as this is frustrating me and making me want to give up learning JS. Thanks

1
  • document.forms.passwordform.yourpassword is an input field, not a string. It doesn’t have a length property. You need .value before .length. Commented Dec 14, 2015 at 4:02

4 Answers 4

2

you need to first prevent the default behavour of form submit .

use

 function validate(e)
     {  
        e.preventDefault();
        var yourpw = document.forms.passwordform.yourpassword.value.length; 
       ... rest of code
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try updating your validate function to as follows:

  function validate(e) {
    e.preventDefault();
    var yourpw = document.getElementsByName('yourpassword')[0].value.length;

    if (yourpw < 5) {
        alert("password is too short");
        return false;
    }

    if (yourpw > 25) {
        alert("your password is too long")
        return false;
    }
}

You need to grab the input fields value, as mentioned, the input field does not have a length (@Xufox) as well as prevent the default behavior for form submission.

Edit:

Full working example:

<!doctype html>
<html>

<head>
    <title>Password Validator</title>
</head>

<body>
    <script>
    function validate(e) {
        e.preventDefault();
        var yourpw = document.getElementsByName('yourpassword')[0].value.length;

        if (yourpw < 5); {
            alert("password is too short");
            return false;
        }

        if (yourpw > 25) {
            alert("your password is too long")
            return false;
        }
    }
    </script>
    <h1>Password Validator</h1>
    <p>Please create a new password below</p>
    <p>Be sure you password follows these guidelines</p>
    <ul>
        <li>No shorter than 5 characters</li>
        <li>No longer than 25 characters</li>
    </ul>
    <br>
    <form name="passwordform" method="post" onsubmit="validate(event);">
        <input type="text" name="yourpassword">
        <br>
        <input type="submit">
    </form>
</body>

</html>

9 Comments

thank you for the quick answer. One question about the "event.preventDefault();" where exactly do i put this in my script?
@colby42536 updated my answer. Ramanlfc demonstrated the proper place to insert the code.
just tried this and, it still wont display any messages. Maybe something is wrong with the html form? I have no other idea, but with the event.preventDefault(); inserted i still don't get alerts showing up.
@colby42536 did you accidentally disable alerts in your browser? Try using console.log('Test: ' + yourPw); in the validation function. Monitor the output in your browser's console (inspect element > console in Google Chrome).
got this message, in the log "TypeError: e is undefined" . I copied the exact code you posted above as well.
|
0

Use this : but give id name yourpassword to input field .. So you can take the value easily

     var yourpw =       document.getElementById("yourpassword").value;
      var len = yourpw.length;
       If ( len < 5 )
        {
       alert ("this is short");
        }
       elseif(len>25)
        {
        alert(" too long")
       } 
      else

        {
         //// your code 


            }

Comments

-1

You can easily validate forms via RegExp.
use this function for validate 5 character limit.

var pw = document.getElementById("yourpassword").value;
function isValidatePW(PW){
var pwRegExp - /\d{5}/
return pwRegExp.test(PW);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.