将Regex实施到Access VBA中以实现密码复杂性 [英] Implementing Regex into Access VBA for Password Complexity

查看:58
本文介绍了将Regex实施到Access VBA中以实现密码复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一个需要密码验证的项目,因为最终用户可以更改其登录密码,但是没有任何验证可确保给定密码在合规范围内.

I'm currently working on a project that requires password validation, as End Users have the ability to change their login password, but there is no validation to ensure that the given password is within compliance.

这有点棘手,因为我对使用VBA和正则表达式还很陌生,而且我找不到想要的答案.

It's been a little tricky, as I'm fairly new to working with VBA and Regular Expressions, and I can't quite find the answer I'm looking for.

该项目有一个Access 2010前端,虽然我有需要验证密码的Regex代码行,但是我仍然不确定如何在函数中实现它并调用它.用户输入密码后,在我的表单中显示.

The project has an Access 2010 Front End, and while I have the line of Regex code that I need to validate the passwords, I'm still a little unsure as to how I can implement it in a function, and call it in my form after the User enters a password.

我一直试图获得工作的代码如下:

The code I've been trying to get work is as follows:

'Password must be between 7 to 12 characters
'Password must contain at least 1 Lowercase, 1 Uppercase, 1 number, and/or 1 Special character


Public Function fValPass(ByVal strPass As String) As Boolean
Dim result As String
Dim RE As Object
'   Sets the regular expression object
Set RE = CreateObject("VBScript.RegExp")
With RE
    .Pattern = "^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{7,12}$"
    '   Does not ignore case
    .IgnoreCase = False
RE.Execute (strPass)

If fValPass = True Then
MsgBox "True", vbOKOnly
Else
    MsgBox "False", vbOKOnly
    End If
    End With
End Function

我知道它可能很笨重,但是我真的很茫然.我将不胜感激,帮助我走上正确的道路,但我意识到这可能是我要打造的相当小众的产品.

I know it might be clunky, but I'm really at a loss here. I'd appreciate any help getting me on the right track, but I realize this might be a rather niche thing I'm trying to build.

推荐答案

您需要返回答案,此外,test方法也是一种返回是/否答案的方法. Execute返回一组匹配的字符串

You need to return the answer, Also, the test method is the one that return you a yes/no answer. Execute returns a set of string that matches

Public Function fValPass(ByVal strPass As String) As Boolean
Dim result As String
Dim RE As Object
'   Sets the regular expression object
Set RE = CreateObject("VBScript.RegExp")
With RE
    .Pattern = "^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{7,12}$"
    '   Does not ignore case
    .IgnoreCase = False
    fValPass  = .Test(strPass) 'affecting to the name of the function is how you return a value in VBA
end with
End Function

P.S.我以为该消息框仅用于测试,因此将其删除

P.S. I assumed that the message box was for tests only and removed it

这篇关于将Regex实施到Access VBA中以实现密码复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆