string validation

40 views
Skip to first unread message

Manuele

unread,
May 12, 2021, 3:22:01 AM5/12/21
to web...@googlegroups.com
Hi!

I have a quite specific question... can anyone help me to implement a
validator that reject all text containing a URL inside?

Some spammers are annoying me registering some fake account introducing
links in registration fields such like first name, last name... I hope
in this way to limit it. Adding Google reCaptcha v2 to the registration
forms didn't help.

Thanks a lot

    Manuele

Raul Monares

unread,
May 12, 2021, 1:08:48 PM5/12/21
to web2py-users
I found this code on the web

# Python code to find the URL from an input string
# Using the regular expression
import re

def Find(string):

# findall() has been used
# with valid conditions for urls in string
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,string)
return [x[0] for x in url]
# Driver Code
print("Urls: ", Find(string))


Chris

unread,
May 12, 2021, 3:41:58 PM5/12/21
to web...@googlegroups.com
Hi, i don’t know a specific way to do that but maybe the following can help.

You can create a function which delete/disable the account with if some url patterns are found in first name and last name

In your models add this function:

def _valid_user(s, i):
    # i —> contains the user id
    # s —> contains a dictionary with the user information
  
    if any(c in s.first_name for c in ['www', 'http', 'https', '%', '/', '.']):
        print('spammer user')
        print('dissabling account’)
        
   
    elif any(c in s.last_name for c in ['www', 'http', 'https', '%', '/', '.']):
        print('spammer user')
        print('dissabling account')


And this after auth declaration:

db.auth_user._after_insert.append(_valid_user)


Or maybe declaring manually the auth table with a custom validator, but I'm not sure if is possible…


Cheers.
Chris.

--
Sent from Canary

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/92075f49-9036-2e9e-28d1-e045f699413d%40gmail.com.

Manuele

unread,
May 13, 2021, 3:56:35 AM5/13/21
to web...@googlegroups.com

Thanks for the valuable advice about the regex!

This is my solution:

https://gist.github.com/manuelep/19a0e9ab6723086fdbf1ffa8e858a5f9

Cheers

    Manuele


Il 12/05/21 19:08, Raul Monares ha scritto:
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages