如何验证密码符合特定条件 [英] How to verify a password meets certain criteria
问题描述
作为基于Web的vb类的作业的一部分,我们现在要设计一个应用程序,要求用户创建一个密码并验证它是否符合以下条件:
-密码长度至少为六个字符。
-密码应至少包含一个数字和至少一个字母字符。
我知道我们正在处理本章的程序和功能,我知道如何处理第一个标准,但我不确定第二部分。这不是我可以从老师那里获得帮助的课程。有哪些方法可以检查第二个要求?这是我到目前为止的代码:
公开 类 Form1
Dim strPassword 正如 字符串
功能 IsValid(strPassword 作为 字符串)作为 Boolean
' 检查用户的密码,看它是否符合
' 要求,并返回合适的消息。
< span class =code-keyword>如果 不 strPassword.Length> = 6 然后
lblStatus.Text = 密码太短。请&
包含至少6个字符。
返回 错误
结束 如果
结束 功能
私人 Sub btnAccept_Click(发件人< span class =code-keyword>作为 对象,e As EventArgs)句柄 btnAccept.Click
' 调用IsValid函数测试用户的密码。
如果 IsValid(txtUserPassword.Text)= True 然后
lblStatus.Text = 密码符合标准。
结束 如果
结束 Sub
私人 Sub btnExit_Click(发件人作为 对象 ,e As EventArgs)句柄 btnExit.Click
' 终止申请。
我 .Close()
结束 Sub
结束 班级
一旦我知道如何处理第二个要求,我就可以返回并输入缺失的部分。感谢您的时间。
人们讨论过许多使用正则表达式的尝试,但这是错误的。按照你这样简单的标准,这很简单。
显然,首先要检查字符串长度。然后你必须有一些局部变量,字节得分= 0;
并按字符累积密码分数。通过所有密码字符循环,如果遇到数字,字母,标点字符等,则添加到分数。不要将得分计算两次,有一些布尔标志(在此解决方案的最简单变体中),例如hasLetter
,hasDigit
并且只检查它是否还没有。
使用System.Char.IsLetter $ c $等方法c>,
System.Char.IsNumber
,System.Char.IsPunctuation
或System.Char .IsLetterOrDigit
等
请参阅: https://msdn.microsoft.com/en-us/library/system.char%28v=vs.110%29.aspx 。
最后,将累计分数与所需分数进行比较。
-SA
由于这是一个作业,我选择只提示你正确的方向,所以你仍然有机会自己学习:-)
如果您无法找到可行的解决方案,我会为您提供进一步的提示。
查看类的静态方法Char
:
https://msdn.microsoft.com/en-us/library/system.char.aspx 一> [ ^ ]
可以使用正则表达式验证器在客户端进行检查。
尝试:
(?= ^。{ 12 , 25 跨度>}
As part of an assignment for my web-based vb class, we are now to design an application that asks the user to create a password and verify that it meets the criteria, which are as follows:
-The password should be at least six characters long.
-The password should contain at least one numeric digit and at least one alphabetic character.
I know that we are working with procedures and functions for this chapter, and I know what to do for the first criteria, but I'm not sure about the second part. This is not the kind of class I can get help from the teacher. What ways are there to check the second requirement? This is my code so far:
Public Class Form1
Dim strPassword As String
Function IsValid(strPassword As String) As Boolean
' Check the user's password to see if it meets the
' requirements, and return a suitable message.
If Not strPassword.Length >= 6 Then
lblStatus.Text = "The password is too short. Please " &
"include at least 6 characters."
Return False
End If
End Function
Private Sub btnAccept_Click(sender As Object, e As EventArgs) Handles btnAccept.Click
' Call the IsValid function to test the user's password.
If IsValid(txtUserPassword.Text) = True Then
lblStatus.Text = "The password meets the criteria."
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Terminate the application.
Me.Close()
End Sub
End Class
I can go back and enter the missing parts once I know what to do for the second requirement. Thank you for your time.
People discussed many attempts to use regular expressions, but this is wrong. In such simple criteria as yours, this is simple.
Apparently, first thing is to check up string length. And then you have to have some local variable,byte score = 0;
and accumulate the password score by its characters. Make a loop by all password characters and add to score if you encounter a digit, a letter, a punctuation character, and so on. Don't count score twice, have some Boolean flags (in simplest variant of this solution), such ashasLetter
,hasDigit
and do the check only if it does not have it yet.
Use the methods likeSystem.Char.IsLetter
,System.Char.IsNumber
,System.Char.IsPunctuation
orSystem.Char.IsLetterOrDigit
, etc.
Please see: https://msdn.microsoft.com/en-us/library/system.char%28v=vs.110%29.aspx.
At the end, compare accumulated score with the required.
—SA
Since this is for an assignment, I choose to only hint you into the right direction so you still have the chance to learn something by yourself :-)
If you should not be able to get to a working solution, I will help you with further hints.
Look into the static methods of the classChar
:
https://msdn.microsoft.com/en-us/library/system.char.aspx[^]
Can Check on Client-Side using Regular Expression Validator.
try:
(?=^.{12,25}
这篇关于如何验证密码符合特定条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!