libs/web: implement minlength(), maxlength() and rangelength() datatypes
[project/luci.git] / libs / web / luasrc / cbi / datatypes.lua
index 71f4a2a..4858619 100644 (file)
@@ -17,7 +17,7 @@ local fs = require "nixio.fs"
 local ip = require "luci.ip"
 local math = require "math"
 local util = require "luci.util"
-local tonumber, type, unpack, select = tonumber, type, unpack, select
+local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select
 
 
 module "luci.cbi.datatypes"
@@ -305,3 +305,41 @@ function max(val, max)
 
        return false
 end
+
+function rangelength(val, min, max)
+       val = tostring(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
+
+function minlength(val, min)
+       val = tostring(val)
+       min = tonumber(min)
+
+       if val ~= nil and min ~= nil then
+               return (#val >= min)
+       end
+
+       return false
+end
+
+function maxlength(val, max)
+       val = tostring(val)
+       max = tonumber(max)
+
+       if val ~= nil and max ~= nil then
+               return (#val <= max)
+       end
+
+       return false
+end
+
+function phonedigit(val)
+       return (val:match("^[0-9\*#]+$") ~= nil)
+end