Page 1 of 2
Regex Length: {3} *OR* {6}?
Posted: Thu May 08, 2008 6:21 pm
by JAB Creations
Here is my current regex for determining if a string is a valid hexadecimal value...
{3} will match strings three characters in length (short-hand color values); how do I allow 3 or 6? {3,6} is a range (you'd think they'd use a dash or something else?
Re: Regex Length: {3} *OR* {6}?
Posted: Thu May 08, 2008 7:06 pm
by s.dot
I think you'll have to go with..
Code: Select all
$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
Not sure though... I've never been the best with regex's

Also... don't forget, some might be uppercase letters. So you'll have to use the i modifier or allow uppercase characters A-F.
Re: Regex Length: {3} *OR* {6}?
Posted: Thu May 08, 2008 7:41 pm
by JAB Creations
Awesome! Thank you Scottayy! I'm totally starting to comprehend the regex anti-maze!
Code: Select all
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
Re: Regex Length: {3} *OR* {6}?
Posted: Sat May 10, 2008 3:03 pm
by prometheuzz
JAB Creations wrote:Awesome! Thank you Scottayy! I'm totally starting to comprehend the regex anti-maze!
Code: Select all
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
Your regex will also match 4 character long hexidecimal strings. I think you meant to do:
Code: Select all
$regex_a = '/^(([0-9a-f]{1,3}){2}|transparent|none)?$/';
You see the slight difference?
Re: Regex Length: {3} *OR* {6}?
Posted: Sun May 11, 2008 6:44 am
by GeertDD
prometheuzz wrote:Your regex will also match 4 character long hexidecimal strings. I think you meant to do:
Code: Select all
$regex_a = '/^(([0-9a-f]{1,3}){2}|transparent|none)?$/';
That regex will match 4 character long hexadecimal strings as well. Actually, 2 char long hexadecimals will even be matched too.
This should be better:
Code: Select all
$regex_a = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
Re: Regex Length: {3} *OR* {6}?
Posted: Sun May 11, 2008 7:06 am
by prometheuzz
GeertDD wrote:...
That regex will match 4 character long hexadecimal strings as well. Actually, 2 char long hexadecimals will even be matched too.
This should be better:
Code: Select all
$regex_a = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
Doh! I didn't know what I was thinking!
; )
Re: Regex Length: {3} *OR* {6}?
Posted: Sun May 11, 2008 11:19 am
by JAB Creations
The regex strings could easily be tested with this page I've been messing with of late on line 120...
Code: Select all
<?php
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
function validate_array($regex, $var)
{
if ($validity != '1')
{
if (!preg_match($regex, $var)) {return false;}
}
return true;
}
foreach($_POST as $key => $value) {if(!validate_array($regex_a,$value)) {$validity = '1'; break;}}
if (!isset($validity))
{
$user_theme = implode(".", $_POST);
$compressed = gzcompress($user_theme, 9);
$var_12 = '24m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd89
24jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf
2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489
cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2
498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc42
89cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489j
c2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj824
89jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j8
9j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j24
89fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m8
9fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492
484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd24
98jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd
8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj24
89jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j
2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28
cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc
4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489
jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj824
89jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89
j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489
fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj
289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd49248
4224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd249
8jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd
8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj248
9jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2
489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj2
8cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489
jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc24
89jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj8
2489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890
j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j
2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224
m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd4
92484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924j
d2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf24
89jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489c
j2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj24
98j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc428
9cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2
489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489
jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j
24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489
fj890j89j24fj82489jc2489123114552345223542342342342343245423524545254244773575367356352
45245245245245243563461';
$var_23 = gzcompress($var_12, 9);
//setcookie('user_theme1',$user_theme,time()+2592000,'/');
//setcookie('user_theme2',$compressed,time()+2592000,'/');
/*
Cookie 4KB Limit = 4085 characters!!!!!!!!!!!!
*/
setcookie('user_theme3',$var_12,time()+2592000,'/');
setcookie('user_theme4',$var_23,time()+2592000,'/');
echo strlen($var_12).'<br /><br />';
echo strlen($var_23);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div.overflow {height: 200px; overflow: auto; width: 40%;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<input name="ce000" value="000" />
<input name="ce001" value="fff" />
<input name="ce002" value="ff0" />
<input name="ce003" value="00f" />
<input name="ce010" value="a1b" />
<input name="ce011" value="7f6" />
<input name="ce012" value="transparent" />
<input name="ce013" value="1f" />
<input name="ce020" value="2f6" />
<input name="ce021" value="3f6" />
<input name="ce022" value="4f6" />
<input name="ce023" value="5f6" />
<input name="ce030" value="6f6" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce090" value="" />
<input name="ce091" value="" />
<input name="ce092" value="" />
<input name="ce093" value="" />
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode(".", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
echo '<div class="overflow">';
//$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
$regex_b = '/^[a-f0-9]{6}$/';
function validate__clientside_array($regex, $var)
{
foreach($_POST as $key => $value)
if ($validity != '1')
{
if (preg_match($regex, $var)) {echo '<b class="good">'.$key.' = '.$var.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$var.'</b> not a <b class="bad">match</b>!</b><br />'; return false;}
}
return true;
}
if(!validate__clientside_array($regex_a,$value)) {$validity = '1';}
echo '</div>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
echo 'hi mom<br />';
?>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
</div>
</body>
</html>
Re: Regex Length: {3} *OR* {6}?
Posted: Mon May 12, 2008 10:11 pm
by JAB Creations
I've adapted PHP to include a function that lets me choose what post data (based on the name attribute's ending number (0-4) and regex filter to use. The problem is everything now matches! I'm currently attempting to
not validate a two or four length string in ANY of the input elements on the second column. The function is currently being called on line 141. The input/name 'ce001' with the value 'fz'
should fail validation for two reasons (1. it's too short (not 3 or 6 characters long) and 2.) it contains an invalid character ('z')). I feel a bit lost now since I don't have any error messages.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div.overflow {height: 200px; overflow: auto; width: 40%;}
form {width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="fz" />
<input name="ce002" value="ff0" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f" />
<input name="ce014" value="1f" />
</div>
<div>
<input name="ce020" value="div" />
<input name="ce021" value="3f6" />
<input name="ce022" value="4f6" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<div>
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce044" value="" />
</div>
<div>
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce054" value="" />
</div>
<div>
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce064" value="" />
</div>
<div>
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce074" value="" />
</div>
<div>
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce084" value="" />
</div>
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
echo '<div class="overflow">';
//$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
function validate_clientside_array($regex, $var, $position)
{
$item = '/'.$position.'$/';
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
echo 'parameter 1 = '.$regex.'<br />';
echo 'parameter 2 = '.$var.'<br />';
echo 'parameter 3 = '.$position.'<br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
//{echo $key."=".$value.'<br />'."\n";}
//if ($validity != '1' && $key != 'ce002')
//echo '<br /><br />666 '.$regex.' 777<br /><br />';
if (preg_match($regex, $var)) {echo '<b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$key.' == '.$var.'</b> not a <b class="bad">match</b>!</b><br />';} // return false;}
}
return true;
}
// if (preg_match("/^abc/", $string)
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';// '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/'; //'/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
//validate_clientside_array('regex_0_selectors',$value,'0');
validate_clientside_array($regex_1_colors,$value,'1');
//validate_clientside_array($regex_1_colors,$value,'3');
//validate_clientside_array($regex_1_colors,$value,'4');
//if(!validate_clientside_array($regex_a,$value,'3')) {$validity = '1';}
echo '</div>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
?>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
</div>
</body>
</html>
Re: Regex Length: {3} *OR* {6}?
Posted: Mon May 12, 2008 11:16 pm
by JAB Creations
Found my fix; how could anyone coding PHP live without echo?
Changed line 128 from...
Code: Select all
if (preg_match($regex, $var)) {echo '<b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
...to...
Code: Select all
if (preg_match($regex, $value)) {echo $value.' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
If at first you don't succeed, echo...echo like you've never echoed before!
Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 2:32 am
by JAB Creations
The regex filter on line 109 is still validating strings 4 or 5 characters in length.
Also I'm not able to either return or global the variable $validity...I'm tired right now...but in essence if a non-match occurs I want to trigger the validation error message instead of having it say all the data validates when it does not.
Any way this is what I have right now and I'm crashing for the night.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div.overflow {float: left; font-size: 12px; height: 200px; overflow: auto; width: 25%;}
form {width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="4444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<input name="ce021" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<div>
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce044" value="" />
</div>
<div>
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce054" value="" />
</div>
<div>
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce064" value="" />
</div>
<div>
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce074" value="" />
</div>
<div>
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce084" value="" />
</div>
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
function validate_clientside_array($regex, $var, $position)
{
$item = '/'.$position.'$/';
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
//$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
echo 'parameter 1 = '.$regex.'<br />';
echo 'parameter 2 = '.$var.'<br />';
echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = 'hi mom';}/*return false;}*/
}
}
echo $validity.' <b><--- (if you don\'t see \'hi mom\' to the left of the arrow then script will falsely proclaim validation, DOH!)</b>';
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';// '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/'; //'/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
//validate_clientside_array('regex_0_selectors',$value,'0');
echo '<div class="overflow">';
validate_clientside_array($regex_1_colors,$value,'1');
echo '</div>';
echo '<div class="overflow">';
validate_clientside_array($regex_1_colors,$value,'3');
echo '</div>';
echo '<div class="overflow">';
validate_clientside_array($regex_1_colors,$value,'4');
echo '</div><br style="clear: both;" />';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
?>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
</div>
</body>
</html>
Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 2:50 am
by prometheuzz
JAB Creations wrote:The regex filter on line 109 is still validating strings 4 or 5 characters in length.

...
Err, I don't think so. Run this:
Code: Select all
#!/usr/bin/php
<?php
$tests = array(
"1f",
"2b4",
"af12",
"abcfa",
"abc4fa",
"abc4fk",
"af12345"
);
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
foreach($tests as $t) {
if(preg_match($regex_1_colors, $t)) {
print "Yes, a match : $t\n";
} else {
print "No match : $t\n";
}
}
/* output:
No match : 1f
Yes, a match : 2b4
No match : af12
No match : abcfa
Yes, a match : abc4fa
No match : abc4fk
No match : af12345
*/
?>
Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 12:57 pm
by JAB Creations
Ok
last regex filter...selectors!
Here is the snippet (with the full page below)...
Code: Select all
$regex_0_selectors = '/([0-9a-z]).,([0-9a-z])/';
I figured out how to create the regex filter for background images! Two numbers, /, three numbers. Here it is...
Code: Select all
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
Any way the CSS selectors regex is on line 120, there are example selectors on column '0', and here is my most current code...
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div {outline: #f00 solid 1px;}
div.overflow {float: left; font-size: 12px; height: 240px; margin: 4px; overflow: auto; width: 25%;}
form {float: left; width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<input name="ce021" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<div>
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce044" value="" />
</div>
<div>
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce054" value="" />
</div>
<div>
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce064" value="" />
</div>
<div>
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce074" value="" />
</div>
<div>
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce084" value="" />
</div>
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<?php
function validate_clientside_array($regex, $position)
{
$item = '/'.$position.'$/';
echo 'parameter 1 = <b>'.$regex.'</b><br />';
echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
if ($validity != '1')
{
if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = '1';}
/*return false;*/
}
}
}
$regex_0_selectors = '/([0-9a-z]).,([0-9a-z])/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/';
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
echo '<div class="overflow"><h3>Column 0 - Selectors</h3>';
validate_clientside_array($regex_1_colors,'0');
echo '</div>';
echo '<div class="overflow"><h3>Column 2 - Background-images</h3>';
validate_clientside_array($regex_2_bgimages,'2');
echo '</div><br style="clear: both;" />';
echo '<div class="overflow"><h3>Column 1 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'1');
echo '</div>';
echo '<div class="overflow"><h3>Column 3 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'3');
echo '</div>';
echo '<div class="overflow"><h3>Column 4 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'4');
echo '</div><br style="clear: both;" />';
echo $validity.' <b><--- (if you don\'t see \'hi mom\' to the left of the arrow then script will falsely proclaim validation, DOH!)</b>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
?>
<div>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
</div>
</body>
</html>
Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 1:53 pm
by JAB Creations
Two changes...
1.) I was validating the wrong regex code (my goof).

2.) I have created a new regex filter for selectors, 0-9, a-z, commas, and periods. The only problem is that blank selectors aren't yet optional. How do I amend my new regex filter to make blank values match?
Here is the new regex filter which in the main file below is located on line 118...
Code: Select all
$regex_0_selectors = '/([0-9a-z]).|,([0-9a-z])/';
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div {outline: #f00 solid 1px;}
div.overflow {float: left; font-size: 12px; height: 240px; margin: 4px; overflow: auto; width: 25%;}
form {float: left; width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<input name="ce021" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<div>
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce044" value="" />
</div>
<div>
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce054" value="" />
</div>
<div>
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce064" value="" />
</div>
<div>
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce074" value="" />
</div>
<div>
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce084" value="" />
</div>
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<?php
function validate_clientside_array($regex, $position)
{
$item = '/'.$position.'$/';
echo 'parameter 1 = <b>'.$regex.'</b><br />';
echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
if ($validity != '1')
{
if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = '1';}
/*return false;*/
}
}
}
$regex_0_selectors = '/([0-9a-z]).|,([0-9a-z])/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/';
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
echo '<div class="overflow"><h3>Column 0 - Selectors</h3>';
validate_clientside_array($regex_0_selectors,'0');
echo '</div>';
echo '<div class="overflow"><h3>Column 2 - Background-images</h3>';
validate_clientside_array($regex_2_bgimages,'2');
echo '</div><br style="clear: both;" />';
echo '<div class="overflow"><h3>Column 1 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'1');
echo '</div>';
echo '<div class="overflow"><h3>Column 3 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'3');
echo '</div>';
echo '<div class="overflow"><h3>Column 4 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'4');
echo '</div><br style="clear: both;" />';
echo $validity.' <b><--- (if you don\'t see \'hi mom\' to the left of the arrow then script will falsely proclaim validation, DOH!)</b>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
?>
<div>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
</div>
</body>
</html>
Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 2:23 pm
by prometheuzz
JAB Creations wrote:Two changes...
1.) I was validating the wrong regex code (my goof).

2.) I have created a new regex filter for selectors, 0-9, a-z, commas, and periods. The only problem is that blank selectors aren't yet optional. How do I amend my new regex filter to make blank values match?
Here is the new regex filter which in the main file below is located on line 118...
Code: Select all
$regex_0_selectors = '/([0-9a-z]).|,([0-9a-z])/';
...
Couple of remarks: the regex-OR (the '|' meta character) has a very low precedence. In other words: "(ab)c|d(ef)" means: "abc OR def" and not "ab followed by a 'c' OR 'd' followed by ef".
So, your regex will match: "one digit or lower case character followed by any character OR a comma followed by a digit or a lower case character". The strings "d!" or ",5" would be matched by your regex, for example.
Note that the dot means "any character" and not the dot itself, if you want to match a dot, you'll need to escape it.
I suspect you want something like this:
Code: Select all
$regex_0_selectors = '/[0-9a-z](\.|,)[0-9a-z]/';
But I have no idea how to extend that regex so that it matches "blank selectors" since I don't know what a selector is, let alone know what a blank selector is. ; )
So, if you show me what a "blank selector" is, and looks like, I can probably help you match it.
Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 4:10 pm
by JAB Creations
*non-EDIT Edit* Forums weren't responsive for about an hour and a friend suggested the following...
This won't match empty strings <input value="" /> being empty. I can also get :: or ::: or ::::: to match.
It's closer then where I was earlier though.
Ok on to my hour ago reply...
Thanks for your suggestion though none of the valid CSS selectors validate with your suggested regex filter.
I have posted a fully functional regex filter tester with text inputs with existing CSS selectors in posts.
But if you're just better with a list here are some examples of valid CSS selectors (keeping in mind I am
not going to need to filter {} curely brackets).
Code: Select all
body h1 h1, h2, h3, h4 div div.border a:hover a:link a:focus