How to set minimum length of password

marse picture marse · Dec 4, 2015 · Viewed 69.9k times · Source

I am currently working on a html form. How do I set the minimum length of the password to 8 so that it will reject any password the user inputs that are less than 8. How to do this?

Here is the code:

<div id="login-form">
            <form method="post">
            <table align="center" width="30%" border="0">
            <tr>
            <td><input type="text" name="email" placeholder="Your Email" required /></td>
            </tr>
            <tr>
            <td><input type="password" name="pass" placeholder="Your Password" required /></td>
            </tr>
            <tr>
            <td><button type="submit" name="btn-login">Sign In</button></td>
            </tr>
            <tr>
            <td><a href="register.php">Sign Up Here</a></td>
            </tr>
            </table>
            </form>
        </div>

Answer

Carlos Mayo picture Carlos Mayo · Dec 4, 2015

If you are using HTML5 you can use the pattern and required attributes for the input tag:

<input type="password" pattern=".{8,}"   required title="8 characters minimum">
<input type="password" pattern=".{8,12}" required title="8 to 12 characters">

Also there is a minlength attribute for input tags but it does not work in some browsers:

<input type="password" minlength="8" required>