Html 如何设置密码的最小长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34084487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:35:37  来源:igfitidea点击:

How to set minimum length of password

htmlformshtml-input

提问by marse

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?

我目前正在处理一个 html 表单。如何将密码的最小长度设置为 8,以便它拒绝用户输入的任何小于 8 的密码。如何执行此操作?

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>

回答by Carlos Mayo

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

如果您使用的是 HTML5,您可以使用输入标签的模式和必需属性:

<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 minlengthattribute for input tags but it does not work in some browsers:

输入标签还有一个minlength属性,但它在某些浏览器中不起作用:

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

回答by Anthony Bastide

Change your button to :

将您的按钮更改为:

<button name="btn-login">Sign In</button>

And add this code JavaScript (using jquery) :

并添加此代码 JavaScript(使用 jquery):

$('button[name="btn-login"]').click(function() {
    if($('input[name="pass"]').val().length < 8) {
        alert('Minimum length = 8');
    } else {
        $('form').submit();
    }
});

Dont forget to add this condition into your PHP code.

不要忘记将此条件添加到您的 PHP 代码中。

回答by AVAVT

You can use the pattern attribute:

您可以使用模式属性:

<input pattern=".{8,}" type="password" name="pass" placeholder="Your Password" required />

回答by efkan

Unfortunately, the minimum length attribute is not supported by the all browsers. Please check this out

不幸的是,并非所有浏览器都支持最小长度属性。请检查一下

Because of this, an alternate method has to used.

因此,必须使用替代方法。

I've chosen the using of regex like the below;

我选择了像下面这样使用正则表达式;

<input type="password" name="pass" title="Password must be 8 characters including 1 uppercase letter, 1 lowercase letter and numeric characters" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" >

回答by Tadej Danev

You could use minlengthand maxlength

你可以使用minlengthmaxlength

As usual, you can use the minlength and maxlength attributes to establish minimum and maximum acceptable lengths for the password. This example expands on the previous one by specifying that the user's PIN must be at least four and no more than eight digits. The size attribute is used to ensure that the password entry control is eight characters wide.

像往常一样,您可以使用 minlength 和 maxlength 属性来确定密码的最小和最大可接受长度。此示例通过指定用户的 PIN 必须至少为四位且不超过八位来扩展上一示例。size 属性用于确保密码输入控件为八个字符宽。

Source

来源

But it works on Chrome only at the moment.

但它目前仅适用于 Chrome。

Or, as someone already mentioned, you could use pattern.

或者,正如有人已经提到的,您可以使用pattern.

If your application has character set restrictions or any other requirement for the actual content of the entered password, you can use the pattern attribute to establish a regular expression to be used to automatically ensure that your passwords meet those requirements.

如果您的应用程序对输入密码的实际内容有字符集限制或任何其他要求,您可以使用模式属性建立一个正则表达式,用于自动确保您的密码满足这些要求。

Source

来源

回答by user12464685

<input type="Password" name="pd" placeholder="Password" pattern=".{8,16}" title="8 or more Character" size=30 pattern="[!@#$%^&*][a-z][A-Z][0-9]" required>

This code worked for me. Try this

这段代码对我有用。尝试这个