libs/web: add range(min,max) datatype validator
This commit is contained in:
parent
b17848e82e
commit
c20dcb3612
3 changed files with 57 additions and 10 deletions
|
@ -155,6 +155,18 @@ var cbi_validators = {
|
||||||
'uciname': function(v)
|
'uciname': function(v)
|
||||||
{
|
{
|
||||||
return (v.match(/^[a-zA-Z0-9_]+$/) != null);
|
return (v.match(/^[a-zA-Z0-9_]+$/) != null);
|
||||||
|
},
|
||||||
|
|
||||||
|
'range': function(v, args)
|
||||||
|
{
|
||||||
|
var min = parseInt(args[0]);
|
||||||
|
var max = parseInt(args[1]);
|
||||||
|
var val = parseInt(v);
|
||||||
|
|
||||||
|
if (!isNaN(min) && !isNaN(max) && !isNaN(val))
|
||||||
|
return ((val >= min) && (val <= max));
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -634,6 +646,14 @@ function cbi_validate_reset(form)
|
||||||
function cbi_validate_field(cbid, optional, type)
|
function cbi_validate_field(cbid, optional, type)
|
||||||
{
|
{
|
||||||
var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
|
var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
|
||||||
|
var vargs;
|
||||||
|
|
||||||
|
if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
|
||||||
|
{
|
||||||
|
type = RegExp.$1;
|
||||||
|
vargs = RegExp.$2.split(/\s*,\s*/);
|
||||||
|
}
|
||||||
|
|
||||||
var vldcb = cbi_validators[type];
|
var vldcb = cbi_validators[type];
|
||||||
|
|
||||||
if( field && vldcb )
|
if( field && vldcb )
|
||||||
|
@ -649,7 +669,7 @@ function cbi_validate_field(cbid, optional, type)
|
||||||
var value = (field.options && field.options.selectedIndex > -1)
|
var value = (field.options && field.options.selectedIndex > -1)
|
||||||
? field.options[field.options.selectedIndex].value : field.value;
|
? field.options[field.options.selectedIndex].value : field.value;
|
||||||
|
|
||||||
if( !(((value.length == 0) && optional) || vldcb(value)) )
|
if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
|
||||||
{
|
{
|
||||||
// invalid
|
// invalid
|
||||||
field.className += ' cbi-input-invalid';
|
field.className += ' cbi-input-invalid';
|
||||||
|
|
|
@ -1358,20 +1358,35 @@ end
|
||||||
|
|
||||||
-- Validate the form value
|
-- Validate the form value
|
||||||
function AbstractValue.validate(self, value)
|
function AbstractValue.validate(self, value)
|
||||||
if self.datatype and value and datatypes[self.datatype] then
|
if self.datatype and value then
|
||||||
if type(value) == "table" then
|
local args = { }
|
||||||
local v
|
local dt, ar = self.datatype:match("^(%w+)%(([^%(%)]+)%)")
|
||||||
for _, v in ipairs(value) do
|
|
||||||
if v and #v > 0 and not datatypes[self.datatype](v) then
|
if dt and ar then
|
||||||
|
local a
|
||||||
|
for a in ar:gmatch("[^%s,]+") do
|
||||||
|
args[#args+1] = a
|
||||||
|
end
|
||||||
|
else
|
||||||
|
dt = self.datatype
|
||||||
|
end
|
||||||
|
|
||||||
|
if dt and datatypes[dt] then
|
||||||
|
if type(value) == "table" then
|
||||||
|
local v
|
||||||
|
for _, v in ipairs(value) do
|
||||||
|
if v and #v > 0 and not datatypes[dt](v, unpack(args)) then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if not datatypes[dt](value, unpack(args)) then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
|
||||||
if not datatypes[self.datatype](value) then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return value
|
return value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -208,3 +208,15 @@ end
|
||||||
function uciname(val)
|
function uciname(val)
|
||||||
return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
|
return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function range(val, min, max)
|
||||||
|
val = tonumber(val)
|
||||||
|
min = tonumber(min)
|
||||||
|
max = tonumber(max)
|
||||||
|
|
||||||
|
if val ~= nil and min ~= nil and max ~= nil then
|
||||||
|
return ((val >= min) and (val <= max))
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue