Forum Moderators: coopster

Message Too Old, No Replies

preg match form validation

         

Doood

5:22 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



I'm using a form to insert the input into a database, but special characters shouldn't be allowed and the only ones being removed with this code are from strip_tags.

Allowed input,
a-z
A-Z (but not two in a row, so no all caps)
numbers 2 and 4
dash
whitespace (other than beginning or end)

This is the best I've come up with so far,


$input = check_input($_POST['input']
(preg_match("/[^a-z24\- ][A-Z]{1}/",$input))

The check_input funtion uses trim and strip_tags.

Whenever I add more to the preg_match it stops matching something else. Sometimes numbers other than 2 or 4 are allowed sometimes they're not, don't know why.

cameraman

6:33 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this about gets it:
#^[\S]([a-z24\-\s]¦[A-Z][^A-Z])+[\S]$#

Don't forget to fix the broken pipe

Doood

7:25 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Hey thanks, that does work when using !preg_match.

One oddity is that every once in a while if you enter something that validates and submit it then if you add one of the characters not allowed and submit again then it will pass right thru without errors.

Like, I'll enter abc and submit. Then change abc to abc# or ab3c and sometimes it passes validation. Kinda strange.

Doood

9:28 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Ah found it. If the [/S] is removed from the beginning and end then it works perfect.