I am having this entry in a JSON file of mine:
{
"rules": [
{
"field": "ingame",
"rules": [
{
"condition": "minLength",
"argument": 1,
"php": false
},
{
"condition": "maxLength",
"argument": 24,
"php": false
},
{
"condition": "zeroRows",
"argument": "SELECT * FROM users WHERE ingame = ?",
"php": true
},
{
"condition": "regexCheck",
"argument": "[a-zA-Z0-9_\\(\\)]{1-24}",
"php": false
}
]
}
]
}
It is correct JSON according to online parsers.
What I mean with the specific regex under "condition": "regexCheck" is: A string consisting of 1-24 symbols that are either being a-z, or A-Z, or 0-9, or _, or ( or ).
How I apply it in JavaScript:
function checkRule(condition, argument, value) {
var pass = false;
var errormessage = "";
switch (condition) {
case "regexCheck":
pass = value.match(new RegExp(argument));
if (!pass) {
errormessage = "Not in expected format";
}
break;
}
return {
pass: pass,
errormessage: errormessage
};
}
However the regex does not seem to be working, and when inspecting the RegExp object I see the following: [a-zA-Z0-9_\(\)]{1-24}.
Does anyone know what is wrong with it?
Also, in PHP I use the following:
function checkRules($rules, $name, $value) {
$pass = false;
$errormessage = "";
if (file_exists("../rules/{$rules}.json")) {
$rules = json_decode(file_get_contents("../rules/{$rules}.json", true));
foreach ($rules->rules as $fvalue) {
if ($fvalue->field == $name) {
foreach ($fvalue->rules as $ruleset) {
switch ($ruleset->condition) {
case "regexCheck":
$pass = preg_match($ruleset->argument, $value);
if (!$pass) {
$errormessage = "Not in expected format";
}
break;
}
if (!$pass) {
break;
}
}
break;
}
else {
$pass = true;
$errormessage = "";
}
}
}
else {
$pass = false;
$errormessage = "Internal error";
}
return array($pass, $errormessage);
}
valuewhen applying the regex?"skiwi2".