(2017-8-6-17:47:18) Form Validation 02
#!/usr/bin/env python#-*-coding:utf-8-*-ImportTornado.ioloopImportTornado.webImportioImportReclassIpfile:regular='.*' #Regular Expressions #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 indicates that a cannot be emptyself.error_dict={} iferror_dict:self.error_dict.update (error_dict)#Update Error TableSelf.required=required#true #传入到对象当中Self.error=None Self.is_valid=False Self.value=NonedefValidate (Self,name,input_value):if notSelf.required:#if it is not true it cannot pass, is not false when it can, that is, required = False #可以为空 Print('self.required', self.required)#TESTTTTTTTTTTTTSelf.is_valid=True Self.value=input_value#values are encapsulated inside #there should be some problems here. Else:#otherwise it is required = True if notInput_value.strip ():ifSelf.error_dict.get ('Required', None):#If you can get the value # Find out if there is a fill error messageself.error=self.error_dict['require']#is directly equal to the given value Else: Self.error="%s is valid"%nameElse: Ret=Re.match (Ipfile.regular, Input_value)ifRet:self.is_valid=True Self.value=Input_valueElse: Ret=re.match (Ipfile.regular,input_value)#match with the given regular Print('ret', ret)#TESTTTTTTTTTTTT ifRet:#Match SuccessSelf.is_valid=True Self.value=input_value#The match succeeds in assigning values to values in the class using the user input value Else: ifSelf.error_dict.get ('valid', None):#If an error message is providedself.error=self.error_dict['valid'] Else: Self.error="%s is valid"%nameclassBaseForm (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}$ ' defcheck_valid (self, handle): Flag=True value_dict={} error_message_dict={} success_value_dict={} forKey, regularinchSelf.__dict__. Items ():#regular inheritance is a regular expression after inheritance is the object ipfile key = ' IP ' Print('Regular', regular)#TESTTTTTTTTTTTTInput_value=handle.get_argument (Key)#gets the value entered by the user Print('Input_value', Input_value)#TESTTTTTTTTTTTTRegular.validate (Key, Input_value)#Executive Ipfile ifRegular.is_valid:success_value_dict[key]=Regular.valuePrint('success_value_dict', success_value_dict)#TESTTTTTTTTTTTT Else: Error_message_dict[key]=Regular.errorPrint('error_message_dict', error_message_dict)#TESTTTTTTTTTTTTflag=False Value_dict[key]=Input_valuereturnflag,success_value_dict,error_message_dictclassHomeform (baseform):def __init__(self):#self.ip=ipfile (required=true)Self.phone=ipfile (required=True)classIndexhandler (tornado.web.RequestHandler):defGet (self):#callback=self.get_argument (' callback ') #self.write ('%s ([11,22,33]); '% callback)Self.render ('index.html') defPost (self, *args, * *Kwargs):#Obj=mainform () #is_valid,value_dict=obj.check_valid (self) #if Is_valid: #print (value_dict) PassclassHomehandler (tornado.web.RequestHandler):defGet (self): Self.render ('home.html') defpost (self): obj=homeform () is_valid,success_dict,error_dict=obj.check_valid (self)Print('error_dict', error_dict)#TESTTTTTTTTTTTT ifIs_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 Validation