From 6780f757d63e60f65a99ae4022f8b0d9c08fee94 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 25 Jun 2012 09:51:59 +0000 Subject: [PATCH] libs/web: implement minlength(), maxlength() and rangelength() datatypes --- libs/web/htdocs/luci-static/resources/cbi.js | 27 +++++++++++++++ libs/web/luasrc/cbi/datatypes.lua | 36 +++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/libs/web/htdocs/luci-static/resources/cbi.js b/libs/web/htdocs/luci-static/resources/cbi.js index 0a49a693a3..655ddbf53a 100644 --- a/libs/web/htdocs/luci-static/resources/cbi.js +++ b/libs/web/htdocs/luci-static/resources/cbi.js @@ -216,6 +216,33 @@ var cbi_validators = { return false; }, + 'rangelength': function(min, max) + { + var val = '' + this; + if (!isNaN(min) && !isNaN(max)) + return ((val.length >= min) && (val.length <= max)); + + return false; + }, + + 'minlength': function(min) + { + var val = '' + this; + if (!isNaN(min)) + return (val.length >= min); + + return false; + }, + + 'maxlength': function(max) + { + var val = '' + this; + if (!isNaN(max)) + return (val.length <= max); + + return false; + }, + 'or': function() { for (var i = 0; i < arguments.length; i += 2) diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua index d3077f954c..48586194a6 100644 --- a/libs/web/luasrc/cbi/datatypes.lua +++ b/libs/web/luasrc/cbi/datatypes.lua @@ -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" @@ -306,6 +306,40 @@ 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 -- 2.30.2