1

I have a form on a site that is collecting user details. There was a fool submitting the form with a name like "Barneycok" from different IP addresses, so I learned how to block that name from going through on the form.

I learned a little regex, just enough to write this little piece:

if (preg_match('/\b(\w*arneycok)\b/', $FirstName)){
$error.= "<li><font face=arial><strong>Sorry, an error occured.  Please try again later.</strong><br><br>"; 
$errors=1;
}

The code has worked perfectly and I never got that name coming through anymore. However, recently, someone is entering a string of numbers on the name field.

The string looks like this:

123456789
123498568
123477698
12346897w

If you notice, the first 4 characters are constant throughout.

So how do I add that in my regex above so that if the name starts with "1234", it will simply match that regex and give the user the error code?

Your help will be greatly appreciated.

Jaime

Jaime
  • 21
  • 6
  • @jamine hope my code will help you out... – Sahil Gulati Apr 30 '17 at 08:31
  • Hey Sahil... thank for your contribution. The problem is that now Barneycok can submit again. I need to simply "add" the 1234 to the regex so that it blocks that and Barneycok also. Make sense? :-) – Jaime Apr 30 '17 at 08:36

3 Answers3

0

The following regex will work.

^1234.*
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • Thank you... but how do I add it to the regex code above? Would the line look like this? if (preg_match('/\b(\w*arneycok)^1234.*\b/', $FirstName)){ – Jaime Apr 30 '17 at 08:28
0

This will match $FirstName which starts with 1234. for matching a specific word like Barneycok you should use this (b|B)arneycok

Regex: ^\s*1234|\b(?:b|B)arneycok\b

1. ^\s*1234 starts with 1234 can contain spaces in starting

2. | is like or condition,

3. \b(?:b|B)arneycok\b matches the word which contains barneycok or Barneycok

Try this code snippet here

if (preg_match('/^1234|\b(?:b|B)arneycok\b/i', $FirstName))
{
    $error.= "<li><font face=arial><strong>Sorry, an error occured.  Please try again later.</strong><br><br>";
    $errors = 1;
}
Community
  • 1
  • 1
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Hi Sahil... thanks for your input. The problem is that this code takes off the "Barneycok", so I'll start getting that fool using that name again. I want to add the other regex to the same sting if possible. Sorry that I didn't clarify that. :-) – Jaime Apr 30 '17 at 08:32
  • @Jaime sorry to ask you again, can you clarify it once? – Sahil Gulati Apr 30 '17 at 08:33
  • Yes, you know how the current code blocks if anyone tries to enter the name "Barneycok" on the First Name field. I need that to stay there and the form should continue to block that. All I need to do is add "1234" so that it blocks ALSO anyone entering "1234558j374" in the first name field. So it should block either of them. Make sense? – Jaime Apr 30 '17 at 08:39
  • @Jaime I have did the same with the post, It will block `Barneycok` and any name starts with `12345` – Sahil Gulati Apr 30 '17 at 08:40
  • Oh... cool! So the secret is in adding the pipe " | " in between the expressions. Nice. Thanks for your help. I'll go ahead and test it out. – Jaime Apr 30 '17 at 08:43
  • @Jaime i want to make a bit careful while using this `(\w*arneycok)`, this can match all `a-z` ,`A-Z` or `0-9` or `_` which is not good you should use like this `(b|B)?arneycok` – Sahil Gulati Apr 30 '17 at 08:58
  • Sahil... You are a wizard man! Thanks for your awesome help! I'll be asking another regex question in a few minutes. I'm sure you'll know how to help me with that one too. If you want of course. :-) – Jaime Apr 30 '17 at 09:01
  • Damn... I can onlly post once every 90 minutes, so I have to wait until I can post my next question. Basically, I have a regex that validates phone numbers and looks like this: if (!preg_match('/^[0-9ext()+ -.:]{9,35}$/i', $Phone)) My question is, how can I also add to block when people enter "555-1212" or "5551212" on the form? – Jaime Apr 30 '17 at 09:11
  • @Jaime not an issue friend... I will surely help you.. once you post a new question, i will there.. :) – Sahil Gulati Apr 30 '17 at 09:12
  • Thanks man... Listen, I have to get some sleep and can't wait 50 minutes until I can post again. I'll post it tomorrow. I hope you're around when I do. Take care and thanks again for your help. You're awesome! – Jaime Apr 30 '17 at 09:15
  • @Jaime I have posted an answer you can check it.. :) – Sahil Gulati May 01 '17 at 03:27
0

For the sake of providing the best possible pattern to protect your site, I'd like to offer this:

/^\s*1234|barneycok/i

This will match a string that has 1234 as its first non-white characters as well as a string that contains the substring barneycok (case insensitively).

Demo Link

You will notice that the pattern:

  • omits the leading word boundary (letting it catch abarneycok),
  • doesn't bother with a non-capturing group with a pipe between B and b (because it is pointless when using the i flag)
  • omits the trailing word boundary (letting it catch barneycoka)
  • uses the i flag so that bArNeYcOk is caught.

You can implement the pattern with:

if(preg_match('/^\s*1234|barneycok/i',$FirstName)){
    $error.="<li><font face=arial><strong>Sorry, an error occurred. Please try again later.</strong><br><br>"; 
    $errors=1;
}

On SO, it is important that the very best answers are posted and awarded the green tick, because sub-optimal answers run the risk of performing poorly for the OP as well as teaching future SO readers bad practices / sloppy coding writing habits. I hope you find this refinement helpful and informative.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136