Solved

PHP preg_match validation

Posted on 2010-11-27
10
437 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

15 Experts available now in Live!

Get 1:1 Help Now