2

There is this input field where I want that user ISN'T able to use following special marks: {}[]$

Right now I have following solution in my code but problem is that it isn't allowing ä, ö, ü or other characters like that.

if (preg_match('/^[-a-zA-Z0-9 .]+$/', $string)  || empty($string)){
echo "Everything ok!";
else{ 
echo "Everything not ok!";}

Because of that, I tried using preg_match('/^[\p{L}\p{N} .-]+$/', $string) because it was said to allow characters from any language but that solution isn't allowing marks like @ and *, which I think may be needed. So any solution which would allow anything except {}[]$ -marks? Any help is much appreciated since I can't figure out what to write to get this working.

Rakshasi
  • 21
  • 1
  • 1
  • 2
  • And why don't you just add the permissable characters to the character class? – mario Mar 13 '14 at 20:25
  • esier to define what you allow, than what you dont –  Mar 13 '14 at 20:26
  • "Anything except `{}[]$`" = `[^{}[\]$]` (surround with `^` and `+$` to test the whole input). Doesn't sound that difficult. – Jon Mar 13 '14 at 20:26
  • @Jon: Thanks for pointing out spelling mistake from one of my previous solutions. I tried that one earlier today but seem that I totally forgot \ -mark. No wonder it gave me errors in first try. Thanks a lot really and sorry for my mistake blindness. – Rakshasi Mar 13 '14 at 20:48

1 Answers1

2

this is how i do it :

if (preg_match('/[^a-zA-Z\d]/', $string)) {
//$string contains special characters, do something.
}
Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35