Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP preg_match validation

Posted on 2010-11-27
10
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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 …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
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.
Suggested Courses

618 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