Solved

PHP preg_match validation

Posted on 2010-11-27
10
439 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 109

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 109

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

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
 

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 109

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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