how to restrict a field to be alphabet only

810 views
Skip to first unread message

Saurabh S

unread,
Oct 12, 2011, 4:41:00 AM10/12/11
to web2py-users
hi i am developing a system in which i require a validation on name
field...validation shoould be : the field should not accept
numbers ...it should only accept alphabets...i know this is possible
using IS_MATCH in web2py...i tried using
db.organisations.name.requires=[IS_MATCH('^[a-z]',
error_message='This
is not a valid name')]
but it is not working
please help me out.....

Johann Spies

unread,
Oct 12, 2011, 7:13:53 AM10/12/11
to web...@googlegroups.com
On 12 October 2011 10:41, Saurabh S <ggtes...@gmail.com> wrote:
 
db.organisations.name.requires=[IS_MATCH('^[a-z]',


This means that you will only names with one character and that character must be in the range a-z.

The following form will accept names with any character except 0-9.

See the

 form = SQLFORM.factory(
        Field('your_name', requires=IS_MATCH('\D+')
        ))

Have a look at a Python regular expression howto eg. http://docs.python.org/dev/howto/regex.html

Regards
Johann
--
 May grace and peace be yours in abundance through the full knowledge of God and of Jesus our Lord!  His divine power has given us everything we need for life and godliness through the full knowledge of the one who called us by his own glory and excellence.
                                                    2 Pet. 1:2b,3a

Anthony

unread,
Oct 12, 2011, 7:25:11 AM10/12/11
to web...@googlegroups.com
I think your regular expression is only matching a single character. Instead, try:

^[a-z]+  # the + says to match one or more characters.

You might want ^[a-zA-Z]+ to match upper or lower case.

Anthony

Anthony

unread,
Oct 12, 2011, 7:32:13 AM10/12/11
to web...@googlegroups.com
Also, I don't think you need the initial ^.

Anthony

unread,
Oct 12, 2011, 7:33:25 AM10/12/11
to web...@googlegroups.com
On Wednesday, October 12, 2011 7:13:53 AM UTC-4, spyker wrote:

        Field('your_name', requires=IS_MATCH('\D+')

Note, \D will match any non-digit character, not just letters.

Johann Spies

unread,
Oct 12, 2011, 8:56:35 AM10/12/11
to web...@googlegroups.com
That is what I said: anything except 0-9.

It allows for surnames like 'Van der Merwe', "O'Sullivan", 'Cronjé'

For finetuning - read the howto.

Anthony

unread,
Oct 12, 2011, 9:00:54 AM10/12/11
to web...@googlegroups.com
Yeah, that's probably better for names. Names can include hyphens as well.

Vinicius Assef

unread,
Oct 12, 2011, 10:37:09 AM10/12/11
to web...@googlegroups.com
But not '*', '%' and so on. hehehe

Saurabh S

unread,
Oct 12, 2011, 8:02:46 PM10/12/11
to web2py-users
thank you all
Reply all
Reply to author
Forward
0 new messages