validation_email.py 790 Bytes
import re
from .base_validation import BaseValidation

class ValidationEmail(BaseValidation):
    def __init__(self, cldr, params = {}):
        super(ValidationEmail, self).__init__(cldr = cldr, params = params)
        
        self.__message          = self.rulemessage or 'Format email :attribute tidak dapat diterima.'
        
    def validate(self):
        ok  = True
        if (self.value is None) or (self.value == ''):
            ok = not self.required
        else:
            val = str(self.value)
            ver = re.match(r'(\w((\.|\-)?\w+)+@(\w(\.|\-)?\w+)+(?:\.\w(\-?\w+)+)+)', val)
            ok = (ver is not None)

        if not ok:
            self.colander_invalid[self.inputname] = self.__message.replace(':attribute', self.title)
            
        return ok