Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,664 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,167 people online right now. Registration is fast and FREE... Join Now!




How preg_match limit number of alphabet to enter

 
Reply to this topicStart new topic

How preg_match limit number of alphabet to enter, alphabet plus numbering

jilong
post 19 Jul, 2007 - 08:34 PM
Post #1


New D.I.C Head

*
Joined: 17 May, 2007
Posts: 32


My Contributions


I get the ideal to fix the number of numbering to enter with 'preg_match', example the code below will fix the number to enter on the form field to 5 digits.

CODE

if (!post || !preg_match("/^\d{5}$/i",$post)){
        return false;
    }


I would like to implement this ideal to my serial code field, the serial code with start with 1 alphabet and follow with 7 digit (example: A1234567 or B1234567).

Below is the code i try, But it i still could enter more than 1 alphabet than follow with numbering (example: aaaaa777)

Hope some one can correct me.

Thanks

CODE

if (!serial || !preg_match("/^[a-z]+[0-9]\d{1,7}$/i",$serial)){

        return false;
    }
User is offlineProfile CardPM

Go to the top of the page

serializer
post 20 Jul, 2007 - 07:37 AM
Post #2


D.I.C Head

**
Joined: 25 Jun, 2007
Posts: 108


My Contributions


QUOTE(jilong @ 19 Jul, 2007 - 09:34 PM) *

CODE

if (!serial || !preg_match("/^[a-z]+[0-9]\d{1,7}$/i",$serial)){

        return false;
    }



That preg will match 1 OR MORE alpha characters (that's what the + means). To match a single character just remove the +.

I can see a couple other problems; your match goes like this:

- One or more alpha chars: [a-z]+ FOLLOWED BY
- One digit: [0-9] FOLLOWED BY
- BETWEEN 1 and 7 digits inclusive: \d{1,7}

If as I understand you simply want to match an Alpha char followed by exactly 7 digits then you just use:

CODE

"/^[a-z]\d{7}$/i"


--serializer
User is offlineProfile CardPM

Go to the top of the page

Styx
post 24 Jul, 2007 - 10:37 AM
Post #3


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


Try preg_match("/^[a-z][0-9]{7}$/i", $var)
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:47AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month