[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP preg_match validation

Posted on 2010-11-27
10
Medium Priority
?
448 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 111

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 111

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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 400 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 111

Accepted Solution

by:
Ray Paseur earned 1600 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 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