Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/41318-how-to-check-string-input-format/
Share on other sites

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.

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. 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.