简体   繁体   English

使用正则表达式在python web.py表单验证中验证密码

[英]Using regex for validating a password in python web.py form validation

I am using python web.py framework for designing small web application, and trying to validate password field with concept that Password length must be more than five characters. 我正在使用python web.py框架设计小型Web应用程序,并尝试使用Password length must be more than five characters.概念来验证密码字段Password length must be more than five characters.

According to the web.py framework we can write regex for validating password field and below is my code 根据web.py框架,我们可以编写用于验证密码字段的正则表达式,以下是我的代码

render = web.template.render('templates/')


urls = (
  '/login',   'Login',
  '/projects',  'Projects',
  '/project_details',  'Project_Details',  
)

app = web.application(urls, globals())
vpass = form.regexp('Must be more than 5', lambda x:int(x)>5)

class Login:

        login_form = form.Form( 
        form.Textbox('username', form.notnull),
        form.Password('password',vpass, description="Password"),
        form.Button('Login'),
        )

    def GET(self):
        form = self.login_form()
        return render.login(form)

    def POST(self):
        if not self.login_form.validates():
            return render.login(self.login_form)

if __name__ == "__main__":
    web.internalerror = web.debugerror
    app.run()  

But when i click on login button no message is displaying on browser " password length must...... " . 但是,当我单击登录按钮时,浏览器上没有显示任何消息“ password length must...... ”。

Whether above regex working ? 以上正则表达式是否有效?

How to implement password validation in web.py frameowrk 如何在web.py frameowrk中实现密码验证

You want to use a Validator , not a rexexp object: 您要使用Validator ,而不要使用rexexp对象:

vpass = form.Validator('Must be more than 5 characters', lambda x:len(x)>5)

A rexexp object expects a python regular expression as the first argument, and an error message as the second argument. rexexp对象期望将python正则表达式作为第一个参数,并将错误消息作为第二个参数。 A password validating regular expression would be something like: 验证正则表达式的密码如下所示:

vpass = form.regexp('.{6,}', 'Must be at least 6 characters')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM