-1

I tried to use regexvalidators from django to validate form so that it does not contain http or https (many spammers using link) but it doesn't work.

from django import forms
from django.core.validators import RegexValidator
from antispam.honeypot.forms import HoneypotField

class ContactForm(forms.Form):
    name = forms.CharField(max_length=50)
    email = forms.EmailField()
    phone = forms.CharField(max_length=11)
    message = forms.CharField(widget=forms.Textarea, validators=[RegexValidator(regex=r'http(s)?|HTTP(s)?', message="I don't accept link", code="invalid")])
    spam_honeypot_field = HoneypotField()

How do I solve this problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
hadi tedi
  • 515
  • 1
  • 4
  • 12

1 Answers1

2

This expression might help you to do so:

^((?!http|https).)+$

enter image description here

enter image description here

Based on Pushpesh's advice, you can use a much simplified expression using a negative lookahead:

^(?!.*\bhttps?\b).+$

which this graph shows how it would work:

enter image description here

Code

import re

string = 'Anything else that you wish except http://someurl.org'
# string = 'Anything else that you wish'
matches = re.search(r'^(((?!http|https).)+)$', string)
if matches:
    print(matches.group(1)+ " is a match")
else: 
    print('Sorry! No matches! Something is not right! Call 911!')

Output

Sorry! No matches! Something is not right! Call 911!
Emma
  • 27,428
  • 11
  • 44
  • 69