limitphp Posted January 27, 2009 Share Posted January 27, 2009 I want to only allow letters and numbers and spaces. There is no minimum, but they can't just have spaces. They have to at least have a number and/or a letter. Here's what I have so far: <?php if(!preg_match('#^[a-z0-9 ]+$#i', $test)) { $message = $message."- Not a valid security answer<BR>"; $highlight_security_answer = 1; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/ Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 I hate to start second guessing myself, but I need yall's advice. Should I even allow spaces in security answers? Spaces can be tricky. What happens if you accidentally put 2 in between two words, or put a space at the front or end of the answer without realizing it, or you do it on purpose, but then forget? I store the security answer as a md5 hash, which I assume turns "answer" into something different than "answer ". Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747762 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 Wait a minute....I can strip spaces before I hash it! alright!.....score one for me! Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747765 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 Ok, so now, I still need yall's help. How do I modify my preg_match to catch nothing but spaces? thanks Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747769 Share on other sites More sharing options...
premiso Posted January 27, 2009 Share Posted January 27, 2009 I do not think this can be done all in one, nor should it. You want to tell the user why the security answer is invalid so they can fix it. I would save the check you have right now for the end then add a check just for the spaces, for the numbers and for the letters. If it is just all spaces then you return false with that error. If it is just all numbers, same thing etc etc. Work at it like that and see what you cannot come up with. Spaces are fine in a security answer some people remember phrases more than a word. Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747772 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 I do not think this can be done all in one, nor should it. You want to tell the user why the security answer is invalid so they can fix it. I would save the check you have right now for the end then add a check just for the spaces, for the numbers and for the letters. If it is just all spaces then you return false with that error. If it is just all numbers, same thing etc etc. Work at it like that and see what you cannot come up with. Spaces are fine in a security answer some people remember phrases more than a word. Ok. Actually, later on, I check each field to see if it was left blank. I do this: if ($security_answer==""){ $message = $message."- Security Answer was left blank<BR>"; $highlight_security_answer = 1; } But, now that I'm thinking about it, if they put spaces, that if statement will not catch it. Is there a "Best Practice" way to check for blank fields? Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747780 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 will this work to catch spaces only? if (trim($security_answer)=="") it seems to work when I'm testing it. Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747786 Share on other sites More sharing options...
premiso Posted January 27, 2009 Share Posted January 27, 2009 Not sure if trim would work, but give that a try. That should trim any leftover white spaces. I do not know if it will just remove the beginning and end and still leave 1. empty may also be an alternative to use. Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747790 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 modify that: if (trim($security_answer, " ")=="") seems to do what I need. Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747791 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747792 Share on other sites More sharing options...
limitphp Posted January 27, 2009 Author Share Posted January 27, 2009 The following things are considered to be empty when using empty(): * 0 (0 as an integer) * "0" (0 as a string) that would be a problem. Quote Link to comment https://forums.phpfreaks.com/topic/142662-solved-need-help-modifying-preg_match-to-prevent-only-spaces/#findComment-747794 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.