Regex for greater than

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
nath2099
Forum Newbie
Posts: 4
Joined: Wed Jan 25, 2012 11:01 pm

Regex for greater than

Post by nath2099 »

I am building a site that will be distributed accross multiple domains. The following code relates to html input fields that may or may not need validation depending on the specific site.

I understand that I can do what I want with just a mathematical greater than in the below code where the <-- REGEX EVALUATION comment is, however I'm trying to make it so that it only has to be edited in the config file.

For this instance of the profileCustom_1 input field I need to test via regex for a value >= 18. So I am needing a regex expression to test that the inputed number is greater than 18..

Code: Select all

//CONFIG FILE
$config['showProfileCustom_1'] = true; 
$config['requireProfileCustom_1'] = true; 
$config['validationRegexProfileCustom_1'] = "'^((?!^[0-4])^(\d+))$^'"; //		<-- REGEX EXPRESSEION HERE



//FORM PAGE
 if ($config['showProfileCustom_1']) { ?>
    <div id='profileCustom_1_div'>
        <label for="profileCustom_1"><?php 
        if ($config['requireProfileCustom_1']) { echo "<span class='requiredInputSpan'>* </span>"; }
        echo $language['profileCustom_1']; ?></label> <?php 
		if (!empty($siteRules['profileCustom_1'])) { echo "<div class='regInfo'>".nl2br($siteRules['profileCustom_1'])."</div>"; } 
		if ($regErrorProfileCustom_1) { ?>
            <div id='profileCustom_1_error_div' class='regErrorDiv'> <?php
                foreach ($regErrorProfileCustom_1 AS $key => $value){
                    echo $value."<br>";
                }?>
            </div><?php
        } ?>
        <input type="text" name="profileCustom_1" id="profileCustom_1" value="<?php if (!empty($regDetails['profileCustom_1'])) { echo $regDetails['profileCustom_1']; }?>" class="
        <?php 
        if ($regError['profileCustom_1'] == 'true') { echo "inputError"; } else { echo "text3";}
        ?>" />
    </div><?php 
} 
					
					
//FORM PROCESSING PAGE
foreach ($_POST as $K => $V) {
	$value = sql_quote(trim($V));
	switch($K) {
		case "profileCustom_1" :
			if ($config['showProfileCustom_1']) {
				$i = 0; 
				$regDetails['profileCustom_1'] = $value;
				if ($config['requireProfileCustom_1'] == true && empty($regDetails['profileCustom_1'])) {
					$regErrorProfileCustom_1[$i] = $language['profileCustom_1']." is required."; 
					$i++;
				}
				else {
					if ($config['validationRegexProfileCustom_1'] != "") {
						if (!preg_match($config['validationRegexProfileCustom_1'], $regDetails['profileCustom_1'])) { //     <-- REGEX EVALUATION
							$regErrorProfileCustom_1[$i] = "This value is invalid."; 
							$i++;
						}
						else {
							$readyForDB[$p]['field'] = "profileCustom_1";
							$readyForDB[$p]['value'] = $regDetails['profileCustom_1'];
							$p++;
						}
					}						
				}
			}
		break;
	}
}
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex for greater than

Post by ragax »

I assume this is an age?
This expression will work for 18 to 129.

Code: Select all

',^1[89]|[2-9][0-9]|1[1-2]\d$,'
Last edited by ragax on Wed Jan 25, 2012 11:58 pm, edited 2 times in total.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex for greater than

Post by ragax »

Example:

Code: Select all

<?php
$regex=',^1[89]|[2-9][0-9]|1[1-2]\d$,';
echo preg_match($regex,'13').'<br />';
echo preg_match($regex,'19').'<br />';
echo preg_match($regex,'42').'<br />';
echo preg_match($regex,'120').'<br />';
?>
Output:
0
1
1
1

Not sure this is what you are asking for, let me know if this is on the right track. :)
Last edited by ragax on Wed Jan 25, 2012 11:57 pm, edited 1 time in total.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Regex for greater than

Post by twinedev »

this one will take any number 18 and over:

Code: Select all

^[1-9]\d{2,}|1[89]|[2-9]\d$
nath2099
Forum Newbie
Posts: 4
Joined: Wed Jan 25, 2012 11:01 pm

Re: Regex for greater than

Post by nath2099 »

That worked great guys, cheers!

Could you explain to me what each part of that is actually doing?
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex for greater than

Post by ragax »

Could you explain to me what each part of that is actually doing?

Code: Select all

',^1[89]|[2-9][0-9]|1[1-2]\d$,'
, --- delimiter: start of regex
^ --- anchor: beginning of string
(?: --- non-capturing group. Three alternations inside, separated by |
Option 1: 1[89] --- a 1, followed by an 8 or a 9
Option 2: [2-9][0-9] --- a 2 to a 9, followed by a 0 to a 9
Option 3: 1[1-2]\d --- a 1 followed by a 1 or 2 followed by any digit.
$ --- anchor: end of string
, --- delimiter: end of regex

Twinedev's expression works great too, it will show you numbers greater than 18 (up to anything). :)
Post Reply