skycruiser Posted March 5, 2007 Share Posted March 5, 2007 I'm workign on a database where the user enters a document number and description. I'd like to check that the document number matches a format (NNN-NNN) and that the description contains only spaces and alphabetical characters. Is there a simple way to do this? I'd guess for the num I break it into 3 parts and check for num, dash and num but I"m not even sure how to do that. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/41318-how-to-check-string-input-format/ Share on other sites More sharing options...
skycruiser Posted March 5, 2007 Author Share Posted March 5, 2007 Just found the is_numeric function Still not sure about the alphabetical part. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/41318-how-to-check-string-input-format/#findComment-200192 Share on other sites More sharing options...
wildteen88 Posted March 5, 2007 Share Posted March 5, 2007 Best of using regex a simple example for checking the document number is in the correct format: $docNum = '026-500'; if(preg_match("#([0-9]+){3}\-([0-9]+)#i", $docNum)) { echo 'Doc number is valid format'; } else { echo ' not valid!'; } You can also do something similar for the description. Quote Link to comment https://forums.phpfreaks.com/topic/41318-how-to-check-string-input-format/#findComment-200196 Share on other sites More sharing options...
skycruiser Posted March 5, 2007 Author Share Posted March 5, 2007 Hmm... I'm still a noob and haven't used that type of syntax before. I guess it's basic stuff but the manual section for this function didn't help a whole lot with the syntax. Would you mind explaining how the syntax works there? I gather the [0-9] is the numeric range, and maybe the {3} is the number of characthers ??? is the "-" just for the dash between the nums? And what is the \ for? And if the {3} is the num of chars why isn't there one for the second [0-9] group? What is the # and #i for? Thanks and again sorry for the dumb questions. Quote Link to comment https://forums.phpfreaks.com/topic/41318-how-to-check-string-input-format/#findComment-200202 Share on other sites More sharing options...
wildteen88 Posted March 6, 2007 Share Posted March 6, 2007 The following syntax is using regular expressions (or regex for short). You can find information on what the characters in the syntax means by reading this page. Regex are very powerful and can be hard to understand when you first start. It took me a while to understand the basic syntax. I made mistake with my regex I provided you -which you spotted the mistake. It is supposed to be this: #([0-9]+){3}\-([0-9]+){3}# I will break it down for you and explain the regex as best as I can. # - This is the starting delimiter. A regex pattern must start and end with a delimiter. The delimiter can be any non-alphanumeric character (any character other than 0-9 and a-z). The ending delimiter must be the same as the starting delimiter. If you use the same character as the delimiters you must escape that character in your pattern, eg: \# ([0-9]+) - This part starts a subpattern (subpatterns delimited with round brackets - ()). This subpattern defines a character class. A character class is defined with square brackets ([]). They allow you to search a range of characters in a string. For this we only want integers ranging from 0 to 9. The + sign means 1 or more. If we didn't have the plus sign it will only matchs 1 number and not the numbers after it. {3} - Allows you to define the min/max range the subpattern matches. In this case we only want 3 subsequent numbers. \- - This basically means match a hyphen (-). Hyphens have a special meaning in regex so we escape it with a forward slash. The last part ([0-9]+){3} is the same as before. The last # is the ending delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/41318-how-to-check-string-input-format/#findComment-201068 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.