-1

I'm trying to make sure a string I'm posting only has alphabetical and numberical letters/numbers + ,.; etc. However all checks I seem to do still bring back à è ò as valid?

Anyone know a solution for this?

Thanks

Saulius Antanavicius
  • 1,371
  • 6
  • 25
  • 55

1 Answers1

0

If you know exactly what characters you want to allow, a regex should be fitting your needs:

// matches exactly the characters you asked for, no more
preg_match(/[a-zA-Z0-9,\.;]*/, $your_string); 

Be sure to escape meta characters, because for example a non-escaped dot (.) will match every character.

Dominik Schreiber
  • 2,629
  • 1
  • 23
  • 33
  • I've updated it to "^/[a-zA-Z0-9\;\'\,\.\;]*/" But when I try the string asdgdfgsasdf.,;'; ir returns false, any ideas what I'm doing wrong? – Saulius Antanavicius Feb 20 '12 at 18:37
  • Try adding the `$` anchor: `"/^[a-zA-Z0-9,\.;']+$/"` – cOle2 Feb 20 '12 at 18:41
  • Seems to me you don't have to escape ";", "'" and "," because they do not have a meta character meaning. And you must start and end with the same character ("/" normally), "^" must be inside the "/"s (see cOle2's regex). – Dominik Schreiber Feb 20 '12 at 18:44
  • 1
    cOle2, tried your one, but still bringing back false :/ Dominik I'm not too familair with regular expressions, so this is proving difficult to udnerstand – Saulius Antanavicius Feb 20 '12 at 18:54
  • Excluding the pattern delimiter `/`, you don't need to escape any character in brace (nor `-` if it is at the end of the char list) so `"/^[a-zA-Z0-9,;:.'-]*$/"` will work. – Visavì Feb 20 '12 at 19:02
  • cOle2's regex works for me (at least for the string you posted). Maybe [this question](http://stackoverflow.com/q/2896450/1168892) could help you more (google showed it to me :) ). – Dominik Schreiber Feb 20 '12 at 19:02
  • OH £@£$@£%£$, had my expression and $str the wrong way around! Haha, thanks for the help guys! – Saulius Antanavicius Feb 20 '12 at 19:12