Solved

PHP preg_match validation

Posted on 2010-11-27
10
438 Views
Last Modified: 2012-06-27
I need a preg_match to make sure it starts with a letter and a max of 8 characters this is for a WHMCS validation.
0
Comment
Question by:zskull88
  • 5
  • 4
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34223644
Untested code, but probably OK in principle:

function validate($str, $len=8)
{
    if (strlen($str) > $len) return FALSE;
    if (!preg_match('/a-z/i', substr($str,0,1)) return FALSE;
    return TRUE;
}
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34223649
Is this the "WHMCS"
http://www.whmcs.com/
0
 

Author Comment

by:zskull88
ID: 34223662
Yes it is Ray.
0
 

Author Comment

by:zskull88
ID: 34223663
Also it needs to allow numbers if possible and caps.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34224002
"... allow numbers if possible and caps."

How?  Please post your test data set including valid and invalid examples so we can see how you expect to process these.  Show us what examples you have for both the good and bad values, then we can show you how to test/select.

Thanks, ~Ray
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zskull88
ID: 34224142
/^[a-z0-9]{2,8}$/i is what I have now for whmcs you can test it on https://billing.pingpulse-servers.com/cart.php?a=add&pid=111
0
 

Author Comment

by:zskull88
ID: 34224148
As you can see with 0asdasd1 as username it is valid and it can't start with a number. and this will only work on username field.
0
 

Author Comment

by:zskull88
ID: 34224174
Ray i think this does it so it works for me at least pleae confirm /^[a-z]|[0-9]{2,8}$/i
0
 
LVL 18

Assisted Solution

by:Sudaraka Wijesinghe
Sudaraka Wijesinghe earned 100 total points
ID: 34224312
I think what you are looking for is /^[a-z]{1}[a-z0-9]{7}$/i
If you want to allow any character after the first, replace [a-z0-9] with .
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 34225718
Just a thought... Learn about TDD.  It will save you a lot of time if you set up your test data - both valid and invalid - before you start trying to write the code.
<?php // RAY_temp_zskull88.php

error_reporting(E_ALL);



$regex

= '/'         // REGEX DELIMITER

. '^'         // START OF STRING

. '[a-z]'     // ALPHABETIC

. '{1}'       // ONE CHARACTER REQUIRED

. '[a-z0-9]'  // ALPHANUMERIC

. '{0,7}'     // ZERO TO SEVEN CHARACTERS REQUIRED

. '$'         // END OF STRING

. '/'         // END OF REGEX DELIMITER

. 'i'         // CASE -INSENSITIVE

;



$valid

= array

( 'A'

, 'A1'

, 'ABCDEFGH'

, 'Z999'

)

;



$bogus

= array

( '31'

, 'A Blank'

, 'muchTooLongForTheRegex'

, NULL

, 'Punc?'

)

;



echo '<br/>TEST FOR VALID';

foreach ($valid as $string)

{

    if (preg_match($regex, $string)) echo "<br/>VALID: $string";

    if (!preg_match($regex, $string)) echo "<br/>BOGUS: $string";

}



echo '<br/>TEST FOR BOGUS';

foreach ($bogus as $string)

{

    if (preg_match($regex, $string)) echo "<br/>VALID: $string";

    if (!preg_match($regex, $string)) echo "<br/>BOGUS: $string";

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now