[web2py] Re: Useful validators IS_LETTERS, IS_DIGITS
Since the two functions are subsets of IS_ALPHANUMERIC(), I'd suggest that the sub-functions be named: IS_ALPHA() and IS_NUMERIC() On Aug 28, 2:56 pm, Jonathan Lundell jlund...@pobox.com wrote: On Aug 28, 2011, at 11:23 AM, Saurabh Sawant wrote: They seem fine. Although, having ready to use validators would save some time for those learning the framework. I personally expected those validators to be already there while I was learning. Trouble is, there's an endless list of pattern expressions that can be useful. IS_MATCH is pretty powerful, and should be in your bag of tricks (in fact, IS_ALPHANUMERIC just calls IS_MATCH). At the very least, consider that you might want a language-dependent IS_LETTERS, or at least one that accepts the common alphabetic variants. However, if you do that, do it this way: IS_MATCH('[0-9]+', strict=True) IS_MATCH('[a-zA-Z]+', strict=True) strict=True forces a $ at the end of the regex. Or you can just include the $. (IS_MATCH is already anchored at the beginning of the string.) On Aug 28, 11:05 pm, Massimo Di Pierro massimo.dipie...@gmail.com wrote: what's wrong with? IS_MATCH('[0-9]+') IS_MATCH('[a-zA-Z]+') On Aug 28, 12:59 pm, Saurabh Sawant ris...@gmail.com wrote: But IS_ALPHANUMERIC by virtue of its name suggests both letters and numbers. Having separate validators for each of the cases would make the code more readable. db.auth_user.first_name.requires=IS_LETTERS() db.auth_user.age.requires=IS_DIGITS()
[web2py] Re: Useful validators IS_LETTERS, IS_DIGITS
But IS_ALPHANUMERIC by virtue of its name suggests both letters and numbers. Having separate validators for each of the cases would make the code more readable. db.auth_user.first_name.requires=IS_LETTERS() db.auth_user.age.requires=IS_DIGITS() is more readable and less ambiguous than db.auth_user.first_name.requires=IS_ALPHANUMERIC(filter='letters') db.auth_user.mobile.requires=IS_ALPHANUMERIC(filter='numbers') On Aug 28, 10:13 pm, Bruno Rocha rochacbr...@gmail.com wrote: Or maybe the IS_ALPHANUMERIC can be extended and receive aditional argument 'filter' to allow only letters or only numbers IS_ALPHANUMERIC(error_message='', filter='numbers') # to allow only digits IS_ALPHANUMERIC(error_message='', filter='letters') # to allow letters can have some filter to allow special, underscores etc... http://web2py.com/book/default/docstring/IS_ALPHANUMERIC On Sun, Aug 28, 2011 at 1:54 PM, Saurabh Sawant ris...@gmail.com wrote: Hi, I think these validators would be a nice addition to the great set of validators we already have. They can be useful for fields for names and phone numbers. class IS_LETTERS(Validator): Checks if field's value consists of all letters example:: INPUT(_type='text', _name='name', requires=IS_LETTERS()) IS_LETTERS()(A) ('A', None) IS_LETTERS()() ('', None) IS_LETTERS()(A_) ('A_', 'enter only letters') IS_LETTERS()(!) ('!', 'enter only letters') def __init__(self, error_message='enter only letters'): IS_MATCH.__init__(self, '^[A-Za-z]*$', error_message) class IS_DIGITS(Validator): Checks if field's value consists of all numeric digits example:: INPUT(_type='text', _name='name', requires=IS_DIGITS()) IS_NUMBERS()(1) ('1', None) IS_NUMBERS()() ('', None) IS_NUMBERS()(A) ('A', 'enter only numbers') IS_NUMBERS()(!) ('!', 'enter only numbers') def __init__(self, error_message='enter only digits'): IS_MATCH.__init__(self, '^[0-9]*$', error_message) Regards, Saurabh Sawant -- -- Bruno Rocha [ About me:http://zerp.ly/rochacbruno] [ Aprenda a programar:http://CursoDePython.com.br] [ O seu aliado nos cuidados com os animais:http://AnimalSystem.com.br] [ Consultoria em desenvolvimento web:http://www.blouweb.com]
[web2py] Re: Useful validators IS_LETTERS, IS_DIGITS
what's wrong with? IS_MATCH('[0-9]+') IS_MATCH('[a-zA-Z]+') On Aug 28, 12:59 pm, Saurabh Sawant ris...@gmail.com wrote: But IS_ALPHANUMERIC by virtue of its name suggests both letters and numbers. Having separate validators for each of the cases would make the code more readable. db.auth_user.first_name.requires=IS_LETTERS() db.auth_user.age.requires=IS_DIGITS() is more readable and less ambiguous than db.auth_user.first_name.requires=IS_ALPHANUMERIC(filter='letters') db.auth_user.mobile.requires=IS_ALPHANUMERIC(filter='numbers') On Aug 28, 10:13 pm, Bruno Rocha rochacbr...@gmail.com wrote: Or maybe the IS_ALPHANUMERIC can be extended and receive aditional argument 'filter' to allow only letters or only numbers IS_ALPHANUMERIC(error_message='', filter='numbers') # to allow only digits IS_ALPHANUMERIC(error_message='', filter='letters') # to allow letters can have some filter to allow special, underscores etc... http://web2py.com/book/default/docstring/IS_ALPHANUMERIC On Sun, Aug 28, 2011 at 1:54 PM, Saurabh Sawant ris...@gmail.com wrote: Hi, I think these validators would be a nice addition to the great set of validators we already have. They can be useful for fields for names and phone numbers. class IS_LETTERS(Validator): Checks if field's value consists of all letters example:: INPUT(_type='text', _name='name', requires=IS_LETTERS()) IS_LETTERS()(A) ('A', None) IS_LETTERS()() ('', None) IS_LETTERS()(A_) ('A_', 'enter only letters') IS_LETTERS()(!) ('!', 'enter only letters') def __init__(self, error_message='enter only letters'): IS_MATCH.__init__(self, '^[A-Za-z]*$', error_message) class IS_DIGITS(Validator): Checks if field's value consists of all numeric digits example:: INPUT(_type='text', _name='name', requires=IS_DIGITS()) IS_NUMBERS()(1) ('1', None) IS_NUMBERS()() ('', None) IS_NUMBERS()(A) ('A', 'enter only numbers') IS_NUMBERS()(!) ('!', 'enter only numbers') def __init__(self, error_message='enter only digits'): IS_MATCH.__init__(self, '^[0-9]*$', error_message) Regards, Saurabh Sawant -- -- Bruno Rocha [ About me:http://zerp.ly/rochacbruno] [ Aprenda a programar:http://CursoDePython.com.br] [ O seu aliado nos cuidados com os animais:http://AnimalSystem.com.br] [ Consultoria em desenvolvimento web:http://www.blouweb.com]
[web2py] Re: Useful validators IS_LETTERS, IS_DIGITS
Hi Massimo, They seem fine. Although, having ready to use validators would save some time for those learning the framework. I personally expected those validators to be already there while I was learning. On Aug 28, 11:05 pm, Massimo Di Pierro massimo.dipie...@gmail.com wrote: what's wrong with? IS_MATCH('[0-9]+') IS_MATCH('[a-zA-Z]+') On Aug 28, 12:59 pm, Saurabh Sawant ris...@gmail.com wrote: But IS_ALPHANUMERIC by virtue of its name suggests both letters and numbers. Having separate validators for each of the cases would make the code more readable. db.auth_user.first_name.requires=IS_LETTERS() db.auth_user.age.requires=IS_DIGITS() is more readable and less ambiguous than db.auth_user.first_name.requires=IS_ALPHANUMERIC(filter='letters') db.auth_user.mobile.requires=IS_ALPHANUMERIC(filter='numbers') On Aug 28, 10:13 pm, Bruno Rocha rochacbr...@gmail.com wrote: Or maybe the IS_ALPHANUMERIC can be extended and receive aditional argument 'filter' to allow only letters or only numbers IS_ALPHANUMERIC(error_message='', filter='numbers') # to allow only digits IS_ALPHANUMERIC(error_message='', filter='letters') # to allow letters can have some filter to allow special, underscores etc... http://web2py.com/book/default/docstring/IS_ALPHANUMERIC On Sun, Aug 28, 2011 at 1:54 PM, Saurabh Sawant ris...@gmail.com wrote: Hi, I think these validators would be a nice addition to the great set of validators we already have. They can be useful for fields for names and phone numbers. class IS_LETTERS(Validator): Checks if field's value consists of all letters example:: INPUT(_type='text', _name='name', requires=IS_LETTERS()) IS_LETTERS()(A) ('A', None) IS_LETTERS()() ('', None) IS_LETTERS()(A_) ('A_', 'enter only letters') IS_LETTERS()(!) ('!', 'enter only letters') def __init__(self, error_message='enter only letters'): IS_MATCH.__init__(self, '^[A-Za-z]*$', error_message) class IS_DIGITS(Validator): Checks if field's value consists of all numeric digits example:: INPUT(_type='text', _name='name', requires=IS_DIGITS()) IS_NUMBERS()(1) ('1', None) IS_NUMBERS()() ('', None) IS_NUMBERS()(A) ('A', 'enter only numbers') IS_NUMBERS()(!) ('!', 'enter only numbers') def __init__(self, error_message='enter only digits'): IS_MATCH.__init__(self, '^[0-9]*$', error_message) Regards, Saurabh Sawant -- -- Bruno Rocha [ About me:http://zerp.ly/rochacbruno] [ Aprenda a programar:http://CursoDePython.com.br] [ O seu aliado nos cuidados com os animais:http://AnimalSystem.com.br] [ Consultoria em desenvolvimento web:http://www.blouweb.com]
Re: [web2py] Re: Useful validators IS_LETTERS, IS_DIGITS
On Aug 28, 2011, at 11:23 AM, Saurabh Sawant wrote: They seem fine. Although, having ready to use validators would save some time for those learning the framework. I personally expected those validators to be already there while I was learning. Trouble is, there's an endless list of pattern expressions that can be useful. IS_MATCH is pretty powerful, and should be in your bag of tricks (in fact, IS_ALPHANUMERIC just calls IS_MATCH). At the very least, consider that you might want a language-dependent IS_LETTERS, or at least one that accepts the common alphabetic variants. However, if you do that, do it this way: IS_MATCH('[0-9]+', strict=True) IS_MATCH('[a-zA-Z]+', strict=True) strict=True forces a $ at the end of the regex. Or you can just include the $. (IS_MATCH is already anchored at the beginning of the string.) On Aug 28, 11:05 pm, Massimo Di Pierro massimo.dipie...@gmail.com wrote: what's wrong with? IS_MATCH('[0-9]+') IS_MATCH('[a-zA-Z]+') On Aug 28, 12:59 pm, Saurabh Sawant ris...@gmail.com wrote: But IS_ALPHANUMERIC by virtue of its name suggests both letters and numbers. Having separate validators for each of the cases would make the code more readable. db.auth_user.first_name.requires=IS_LETTERS() db.auth_user.age.requires=IS_DIGITS()