首页 > 代码库 > Html form表单验证

Html form表单验证

(2017-8-6 17:47:18) 表单验证 02

    

技术分享
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web
import io
import re
class Ipfile:
    REGULAR=.* # 正则表达式
    # regular="^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"
    def __init__(self,error_dict=None,required=True): #requried 表示不能为空
        self.error_dict={}
        if error_dict:
            self.error_dict.update(error_dict) # 更新错误表
        self.required=required #true  #传入到对象当中
        self.error=None
        self.is_valid=False
        self.value=None
    def validate(self,name,input_value):
        if not self.required : # 如果是not true就不能通过,是not false的时候就能,即 required = false #可以为空
            print(self.required, self.required)  # testttttttttttt
            self.is_valid=True
            self.value=input_value # 值封装在里面

            # 这里应该有些问题
        else: #否则就是 required = True
            if not input_value.strip():
                if self.error_dict.get(required,None): # 如果能获取到值 # 找是否有填错误信息
                    self.error=self.error_dict[require] # 就直接等于给的值
                else:
                    self.error = "%s is valid" % name
            else:
                ret = re.match(Ipfile.REGULAR, input_value)
                if ret:
                    self.is_valid=True
                    self.value=input_value
                else:
                    ret=re.match(Ipfile.REGULAR,input_value) #跟给定正则匹配
                    print(ret, ret)  # testttttttttttt
                    if ret: #匹配成功
                        self.is_valid=True
                        self.value=input_value #匹配成功就用用户输入的值赋值给类里面的value
                    else:
                        if self.error_dict.get(valid,None): # 如果有提供错误信息
                            self.error=self.error_dict[valid]
                        else:
                            self.error="%s is valid" % name
class Baseform(object):
    def __init__(self):
        pass
        # self.host = "(.*)"
        # self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"
        # self.port = ‘(\d+)‘
        # self.phone = ‘^1[3|4|5|8][0-9]\d{8}$‘

    def check_valid(self, handle):
        flag=True
        value_dict={}
        error_message_dict={}
        success_value_dict={}
        for key, regular in self.__dict__.items(): # regular 继承前是正则表达式  继承后是对象ipfile key = ‘ip‘
            print(regular, regular)  # testttttttttttt
            input_value=http://www.mamicode.com/handle.get_argument(key) #获取用户输入的值
            print(input_value,input_value) #testttttttttttt
            regular.validate(key, input_value)  # 执行ipfile
            if regular.is_valid:
                success_value_dict[key]=regular.value
                print(success_value_dict, success_value_dict)  # testttttttttttt
            else:
                error_message_dict[key]= regular.error
                print(error_message_dict,error_message_dict) #testttttttttttt
                flag=False

            value_dict[key]=input_value
        return flag,success_value_dict,error_message_dict

class Homeform(Baseform):
    def __init__(self):
        # self.ip=Ipfile(required=True)
        self.phone=Ipfile(required=True)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        # callback=self.get_argument(‘callback‘)
        # self.write(‘%s ([11,22,33]);‘ % callback)
        self.render(index.html)
    def post(self, *args, **kwargs):
        # obj=MainForm()
        # is_valid,value_dict=obj.check_valid(self)
        # if is_valid:
        #     print(value_dict)
        pass
class Homehandler(tornado.web.RequestHandler):
    def get(self):
        self.render(home.html)
    def post(self):
        obj=Homeform()
        is_valid,success_dict,error_dict=obj.check_valid(self)
        print(error_dict,error_dict)  #testttttttttttt
        if is_valid:
            print(sccess,success_dict)
        else:
            print(error,error_dict)
            self.render(home.html)
        # print(is_valid,success_dict,error_dict)


settings = {
    template_path: views,
    static_path: statics,
    static_url_prefix: /statics/,
}

application = tornado.web.Application([
    (r"/index", IndexHandler),
    (r"/home", Homehandler),
], **settings)


if __name__ == "__main__":
    application.listen(8088)
    tornado.ioloop.IOLoop.instance().start()
View Code

 

         

Html form表单验证