Jump to content

[SOLVED] Need Help Modifying preg_match to Prevent Only Spaces


limitphp

Recommended Posts

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

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 ".

 

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.

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?

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.