PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have an input form that sends data to my database. This is fine, the problem is my extremely basic filtering. I am trying to make it so only letters ( a-z A-Z ) and numbers are allowed through. I think i am using preg_match incorrectly because it only checks if the first digit is valid or invalid. It does not validate the whole string.
Maybe i don't understand how preg_match works. With the code you provided, if i type in #, it says it's invalid, which is good. but if i type #a it says it's valid. I don't get it.
The reason why the regex allowed #a is because it only have to find a match somewhere inside the string. For instance
Regex: /abc/ you should learn your abcs - match found
To prevent this you should use the start (^) and end ($) assertions. Which basically say there should be nothing before and nothing after, respectively.
Regex: /^abc$/ you should learn your abcs - no match found abc - match found
ole wrote:The reason why the regex allowed #a is because it only have to find a match somewhere inside the string. For instance
Regex: /abc/ you should learn your abcs - match found
To prevent this you should use the start (^) and end ($) assertions. Which basically say there should be nothing before and nothing after, respectively.
Regex: /^abc$/ you should learn your abcs - no match found abc - match found