DLR Posted June 16, 2009 Share Posted June 16, 2009 Hi I'm clearly mixied up in my logic. I'm try to see if there is a match with any unwanted characters - but preg_match simply ignores my seach parameters - so I have made a mistake somewhere . . . but where? Can you help me? I get the same result for these two differing inputs : - $_POST['branch'] = !@#$%^& or $_POST['branch'] = Melrose Arch if(preg_match('/[^a-zA-Z0-9_'-]/',$_POST['branch'] )) { global $errors; $errors[] = "Invalid code in Branch name"; } Quote Link to comment Share on other sites More sharing options...
Alex Posted June 16, 2009 Share Posted June 16, 2009 Do you mean something like this?: if(preg_match("/^[a-zA-Z0-9_'-]*$/",$_POST['branch'] )) { //No unwanted characters. } Quote Link to comment Share on other sites More sharing options...
DLR Posted June 16, 2009 Author Share Posted June 16, 2009 HI, thanks. I've tested your suggestion and it seems to make no difference. Also I understand that the "^" needs to be inside like this [^a-zA-Z0-9] , as opposed to ^[a-zA-Z0-9] . . is this correct? Also, my understanding of the "$" sign is to have the pattern matched at the end of a string . . . is this also correct? ??? David Quote Link to comment Share on other sites More sharing options...
Alex Posted June 16, 2009 Share Posted June 16, 2009 I'm not really sure what you need this for. So many explaining what it needs to do would be better. Aren't you trying to validate a whole string? Or what. Quote Link to comment Share on other sites More sharing options...
DLR Posted June 16, 2009 Author Share Posted June 16, 2009 HI thanks for the comment. I want to identify if there are any characters that should not be there - like < ' " !@#$%^& etc. If the $_POST['branch '] has any of these, I'll assume that someone is messing with the input and exclude the post. So I assumed that [^a-zA-Z0-9] would match any thing that is NOT a letter or a number. Quote Link to comment Share on other sites More sharing options...
Alex Posted June 16, 2009 Share Posted June 16, 2009 What I gave you should work. Here's some examples: $str = 'test)'; $pattern = "/^[a-zA-Z0-9_'-]*$/"; if(preg_match($pattern, $str, $matches)) echo 'Validates'; else echo 'Does not validate'; Output: Does not validate $str = 'all_acceptable_characters'; $pattern = "/^[a-zA-Z0-9_'-]*$/"; if(preg_match($pattern, $str, $matches)) echo 'Validates'; else echo 'Does not validate'; Output: Validates Try it yourself. Quote Link to comment Share on other sites More sharing options...
DLR Posted June 16, 2009 Author Share Posted June 16, 2009 yes it does work. Thank you Clearly my problem lies elsewhere! Thank you for assistance. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 16, 2009 Share Posted June 16, 2009 Something along the lines of: //$_POST['branch'] = '!@#$%^&'; $_POST['branch'] = 'Melrose Arch'; echo (preg_match('#^[a-z0-9_ -]+$#i', $_POST['branch']))? $_POST['branch'] . '= Valid!' : $_POST['branch'] . '= Invalid!'; ? Granted, I didn't add the single quote inside the character class, since: I want to identify if there are any characters that should not be there - like < ' " !@#$%^& etc. So I am assuming you only want a-z (upper / lowercase), 0-9, _ and a dash.. I added a space in the character class as well ( since you have the example $_POST['branch'] = Melrose Arch (which should be $_POST['branch'] = 'Melrose Arch' ). I also used the + quantifier instead of *, as I suspect you would want at least a single character minimum (but something tells me an interval would be better: {min, max}) EDIT - Looking at your original line: [font=courier new][size=12px]if(preg_match('/[^a-zA-Z0-9_'-]/',$_POST['branch'] )) {[/size][/font] Note that since you are using single quotes to encapsulate your pattern (which is perfectly fine), you would need to escape that quote inside the character class (even though you don't seem to want the quote as I quoted you at the top of this post). Otherwise, the regex engine thinks you are done with the pattern. And as AlexWD pointed out, ^ and $ are necessary to check from beginning to end of string. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 16, 2009 Share Posted June 16, 2009 if(preg_match('/[^a-zA-Z0-9_'-]/',$_POST['branch'] )) { global $errors; $errors[] = "Invalid code in Branch name"; } The only problem with that code is that you forgot to escape the single quote (or remove it if you don't want to allow it), as nrg also mentioned. And yeah, you should also add a space inside the character class to allow spaces. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.