Jump to content

Need test specific format 1,2,3


meltingpoint

Recommended Posts

Greetings regex experts.  I have an input form that assigns levels to each user.  There are 7 total.  I need to test the input so that it is loaded properly.

 

The edit box entry should look like :  1,2,3,4,5,6,7

- it needs to be separated by commas and need to end with no comma.

- it can be 1,2,3  or  1,4,7  or all of them or only one or two

- the important thing is that it be 1-7, each separated by commas and end with no comma.

 

Any help would be appreciated.

Tks

Link to comment
Share on other sites

~^[1-7](,[1-7])*$~

 

This will do it, but if you want to be sure, you're best off using something like the following. It will make sure each value is only used once

 

<?php

$input = '1,2,3,,7,7,7,7,8,9,10';

$vals = array_unique(explode(',', $input));

foreach($vals as $key => $val) {
if($val < 1 || $val > 7 || (int)$val != $val) {
	unset($vals[$key]);
}
}
$output = implode(',', $vals);
echo $output;

Link to comment
Share on other sites

Ok-

 

entering a permission level of 1 - does not work

entering a permission level of 1,2 or 1,2,3 or 1,2,3,4  works fine

entering a permission level of 1,2,3,4,5  does not work nor does 1,2,3,4,5,6 or 1,2,3,4,5,6,7

entering a permission level of 2,3,4,5  works but 2,3,4,5,6 does not

 

So to recap- a person will have at least 1 permission level 1-7.  And can have any combination of all 7 permission levels i.e. 1,3,5,7 or 2,4,5 etc.....

Link to comment
Share on other sites

I have a suspicion that you are really trying to do this the wrong way. Are you storing the value of "1,2,3" as a value into the database? Besides, if there are 7 possible values, just include 7 checkboxes for input (named as an array). That would make a much more elegant solution. Then just implode the POST values

 

Form:

Level 1 <input type="checkbox" name="levels[]" value="1' />
Level 2 <input type="checkbox" name="levels[]" value="2' />
Level 3 <input type="checkbox" name="levels[]" value="3' />
...etc.

 

PHP

$levels = implode(','$_POST['levels']);

 

however, here is a solution for you. The one thing it does not do is prevent a user from entering the same value twice (as long as the value is between 1-7), but it does ensure it matches the pattern you specified and does not contain more than seven digits.

function checkLevels($levelsString)
{
    return (preg_match("#^[1-7]{1}(,[1-7]{1}){0,6}$#", $levelsString)!==0);
}

//Testing array
$testvalues = array(
    "1",
    "1,2",
    "11,3,4",
    "1,2,,4",
    "1,2,",
    "1,2,3,4,5,6,7",
    "",
    "1,2,3,8",
    "1,2,3,4,5,6,7,7"
);
//Test process
foreach($testvalues as $value)
{
    $response = (checkLevels($value)) ? 'Pass' : 'Fail';
    echo "[{$value}]: {$response}<br>\n";
}

 

Test results

[1]: Pass
[1,2]: Pass
[11,3,4]: Fail
[1,2,,4]: Fail
[1,2,]: Fail
[1,2,3,4,5,6,7]: Pass
[]: Fail
[1,2,3,8]: Fail
[1,2,3,4,5,6,7,7]: Fail

Link to comment
Share on other sites

Why not store the access levels in a bitwise method?

 

http://www.litfuel.net/tutorials/bitwise.htm

 

1 - 1

2 - 2

3 - 4

4 - 8

5 - 16

6 - 32

7- 64

 

So someone who's 1,3,5 would be 1+4+16 = 21. You can check if someone has level 5 by doing

 

If( $accessInteger & 16 ) {

 

}

 

The website does a great job of explaining what bitwise is and gives specific examples relevant to this discussion

 

Storing in int rather than varchar saves you a TON of database space as well, and makes things faster :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.