添加最小密码长度和复杂性控制

有人可以告诉我如何向我的网站添加最小密码长度和复杂性的控件


这是数据库


CREATE TABLE Users(


id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

username VARCHAR(50) NOT NULL,

password VARCHAR(10) NOT NULL,

birthday DATE

);


CREATE TABLE dreams(


dream_id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(dream_id),

content text NOT NULL,

user_id INT,

FOREIGN KEY(user_id) REFERENCES Users(id)


);

我希望当用户注册并尝试创建密码时,他们会收到一条消息,告诉他们密码太弱或者必须由字母和数字组成..等


我可以通过数据库做些什么吗?如果在其他地方请帮助我是初学者我使用nodejs-html-CSS-JavaScript谢谢。


三国纷争
浏览 151回答 3
3回答

呼唤远方

首先,您应该将验证设置为同时在服务器端和客户端工作。客户端,主要是为了用户体验,因为精通开发的人总是可以绕过浏览器中的任何内容。但是,根据您的声明,您希望有一条漂亮的用户消息并显示该消息,无论客户端是什么,这将帮助您的用户更好地了解如何使用您的表单。服务器端验证也应该到位,以防止错误数据保存在数据库中。简而言之,您需要他们携手合作。您不需要任何特殊的 js 库来进行最基本的验证。HTML5 将为您提供显示许多需求所需的内容,而无需额外的 JavaScript。但是,如果您不喜欢 HTML5 验证的外观,则需要使用某种 JavaScript。但是,我总是会从使用它开始,因为它将帮助您的应用程序更轻松地满足可访问性标准,并且从那里,您可以通过在其上添加更精美的基于 javascript 的东西来进行迭代,使其看起来更好。该片段将有一个警告叠加层,该叠加层不会出现在您的网站上。<form action="">  <label for="pwd">Password:</label>  <input type="password" id="pwd" name="pwd"  pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"  title="Must contain at least one  number and one uppercase and lowercase letter, and at least 8 or more characters">  <input type="submit"></form>现在,在后端,您应该在处理表单的代码中创建一个函数,以在将其与其他数据库规则一起保存到数据库之前重新检查验证。如果由于用户或某些错误以某种方式允许绕过验证而导致此验证失败,您将在表单响应中发回一条失败消息,前端将在尝试保存到数据库失败后捕获并显示该消息。要回答具体如何进行,需要更多有关如何设置应用程序的信息。您还可以在数据库中创建一个表来存储验证规则,并可以通过某种 API 将这些规则传递到前端,以便前端和后端函数可以共享相同的规则,而不必硬编码相同的正则表达式或无论你正在处理什么规则。换句话说,这两个验证的单一事实点——用户体验检查和数据完整性检查。

长风秋雁

我正在使用验证密码来检测检测到的弱密码或强密码:我有两个例子:This is Pure Javascriptvar code = document.getElementById("password");var strengthbar = document.getElementById("meter");var display = document.getElementsByClassName("textbox")[0];code.addEventListener("keyup", function() {&nbsp; checkpassword(code.value);});function checkpassword(password) {&nbsp; var strength = 0;&nbsp; if (password.match(/[a-z]+/)) {&nbsp; &nbsp; strength += 1;&nbsp; }&nbsp; if (password.match(/[A-Z]+/)) {&nbsp; &nbsp; strength += 1;&nbsp; }&nbsp; if (password.match(/[0-9]+/)) {&nbsp; &nbsp; strength += 1;&nbsp; }&nbsp; if (password.match(/[$@#&!]+/)) {&nbsp; &nbsp; strength += 1;&nbsp; }&nbsp; if (password.length < 6) {&nbsp; &nbsp; display.innerHTML = "minimum number of characters is 6";&nbsp; }&nbsp; if (password.length > 12) {&nbsp; &nbsp; display.innerHTML = "maximum number of characters is 12";&nbsp; }&nbsp; switch (strength) {&nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; strengthbar.value = 0;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; strengthbar.value = 25;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; strengthbar.value = 50;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; strengthbar.value = 75;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 4:&nbsp; &nbsp; &nbsp; strengthbar.value = 100;&nbsp; &nbsp; &nbsp; break;&nbsp; }}<body>&nbsp; <form class="center-block">&nbsp; &nbsp; <input type="text" id="password" autocomplete="off" class="form-control input-lg">&nbsp; &nbsp; <progress max="100" value="0" id="meter"></progress>&nbsp; </form>&nbsp; <div class="textbox text-center"> password </div></body>这是 jQueryfunction ValidatePassword() {&nbsp; /*Array of rules and the information target*/&nbsp; var rules = [{&nbsp; &nbsp; &nbsp; Pattern: "[A-Z]",&nbsp; &nbsp; &nbsp; Target: "UpperCase"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Pattern: "[a-z]",&nbsp; &nbsp; &nbsp; Target: "LowerCase"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Pattern: "[0-9]",&nbsp; &nbsp; &nbsp; Target: "Numbers"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Pattern: "[!@@#$%^&*]",&nbsp; &nbsp; &nbsp; Target: "Symbols"&nbsp; &nbsp; }&nbsp; ];&nbsp; //Just grab the password once&nbsp; var password = $(this).val();&nbsp; /*Length Check, add and remove class could be chained*/&nbsp; /*I've left them seperate here so you can see what is going on */&nbsp; /*Note the Ternary operators ? : to select the classes*/&nbsp; $("#Length").removeClass(password.length > 6 ? "glyphicon-remove" : "glyphicon-ok");&nbsp; $("#Length").addClass(password.length > 6 ? "glyphicon-ok" : "glyphicon-remove");&nbsp;&nbsp;&nbsp; /*Iterate our remaining rules. The logic is the same as for Length*/&nbsp; for (var i = 0; i < rules.length; i++) {&nbsp; &nbsp; $("#" + rules[i].Target).removeClass(new RegExp(rules[i].Pattern).test(password) ? "glyphicon-remove" : "glyphicon-ok");&nbsp;&nbsp; &nbsp; $("#" + rules[i].Target).addClass(new RegExp(rules[i].Pattern).test(password) ? "glyphicon-ok" : "glyphicon-remove");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /*Bind our event to key up for the field. It doesn't matter if it's delete or not*/&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; $("#NewPassword").on('keyup', ValidatePassword)&nbsp; &nbsp; });.glyphicon-remove {&nbsp; color: red;}.glyphicon-ok {&nbsp; color: green;}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><div class="form-group has-feedback">&nbsp; <input class="form-control" id="NewPassword" placeholder="New Password" type="password">&nbsp; <span class="glyphicon glyphicon-lock form-control-feedback"></span></div><div id="Length" class="input glyphicon-remove">Must be at least 7 charcters</div><div id="UpperCase" class="glyphicon glyphicon-remove">Must have atleast 1 upper case character</div><div id="LowerCase" class="glyphicon glyphicon-remove">Must have atleast 1 lower case character</div><div id="Numbers" class="glyphicon glyphicon-remove">Must have atleast 1 numeric character</div><div id="Symbols" class="glyphicon glyphicon-remove">Must have atleast 1 special character</div>

繁星点点滴滴

您可以在 javascript 代码中使用 jquery.validate,<script>&nbsp; &nbsp; $("#form1").validate({&nbsp; &nbsp; &nbsp; &nbsp; rules:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required:true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minlength:8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; messages:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required:"Password is required",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minlength:"Passowrd must be atleast 8 characters long"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });</script>密码是输入字段中的名称属性,对于数据库中的图片,您可以使用 varbinary(max)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript