http://www.w3resource.com/javascript/form/string-length.php

In the above link I see two different JS areas, Javascript function to restrict length of user input, & Javascript code. All I'm trying to do is validate that the users password of choice is at least x characters in length. Which one of these examples is what I'm looking for? Thanks.

Recommended Answers

All 5 Replies

The first example is fine. If you want at least say 8 characters then...

if(userInput.length >= 8 { 

On that site, they show you two different approaches with regard to checking...

The first is basically whether the length is greater than min AND less than the max number. If the result is true then that's good. Else, take some action.

The second example is whether the length is less than the min or greater than the max. If the result is good, then that's bad. Take the appropriate action for non compliance.

echo "<tr><td><input type='submit' value='submit' onclick='stringlength(document.form1.text1,6,8)'></td></tr>";

& the file

function lengthRange(inputtxt, minlength, maxlength)  
{     
   var userInput = inputtxt.value;    
   if(userInput.length >= minlength && userInput.length <= maxlength)  
      {       
        return true;      
      }  
   else  
      {       
    alert("Please input between " +minlength+ " and " +maxlength+ " characters");         
        return false;     
      }    
}

Are not having any affect on my password input. Any idea?

Well, I've made some changes to your example, but you should note that the function you are calling and the name of the function in your script is different, so that's why nothing good or bad is happening in your case.

Try this..

<!DOCTYPE html>
<html>
<head>
 <title>Demo</title>
 <script>
 function validate(form, minlength, maxlength) {     
      var userInput = form.elements[0].value
      if(userInput.length >= minlength && userInput.length <= maxlength)  {       
        return true;      
      }  else  {       
         alert("Please input between " + minlength + " and " + maxlength + " characters");         
        return false;     
      }    
 }
 </script>

</head>
<body>
 <form id="form1" action="#" >
   <input type='text' /><br />
   <input type='submit' value='submit' onClick='return validate(this.form,6,8)'>
 </form>
</body>
</html>
if($row['password'] == $passwd)
        {
        echo "<html>";
        echo "<body>";
        echo "<script src='check.js'></script>";
        echo "<form id='form1' action='changepassword.php' method='post'>";
        echo "<table>";
        echo "<tr><td>";
        echo "New Password<center><input type='text' name='newpassword1'></td></tr></center>";
        echo "<tr><td>";
        echo "Re-Enter Password<center><input type='text' name='newpassword2'></td></tr></center>";
        echo "<tr><td><input type='submit' value='submit' onclick='return validate(this.form,6,8)'></td></tr>";
        echo "</table>";
        echo "</form>";
        echo "</body>";
        echo "</html>";
        }

What am I doing wrong?

function validate(form, minlength, maxlength) 
{
var userInput = form.elements[0].value

if(userInput.length >= minlength && userInput.length <= maxlength)
{
    return true;
}
else
{
    alert("Please input between " + minlength + " and " + maxlength + " characters");
return false;
}

}

You are missing a closing brace at the end of your javascript. If you use a web browser's dev tools, you can look at the errors generated in the console log.

I see that you started a new thread. Please try to complete one thread before you start another. I already replied in the other thread, otherwise it would have been deleted and we could have continued working with you here...

Here is the link for anyone else that if following this discussion..

http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/465310/java-script-password-length

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.