If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
from gluon.validators import Validator


class CLEANUP_JUST_NUMBERS(Validator):
    '''Filters a field stripping out non-numeric chars from it.
    '''
    def __call__(self, value):
        chars = [c for c in value if c.isdigit()]
        return (''.join(chars), None)

Related slices

Comments (1)

  • Login to post



  • 0
    rochacbruno 11 years ago

    Is it really needed to have the __init__ method? I did not tested, but, the init is doing nothing.

     

    requires=CLEANUP_JUST_NUMBERS() will create an object without the need to pass any argument.

    Also I never created a subclass of Validator. I always use just "object" and it works. There is some advantage on subclassing Validator?

     

    BTW: Thanks for the slice!

    replies (1)
    • viniciusban 11 years ago

      You're right. We don't need __init__(). Code updated. Thanks.

      I subclass Validator just because all Web2py native validators do this way.


Hosting graciously provided by:
Python Anywhere