/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Validator extensions for time
* @for Validator
* @namespace jsbeans
* @static
* */
jsbeans.Validator.time = {
/**
* Default messages for Validator.time extension
* @property time.messages
* @type JSON
* @static
* */
messages: {
en: "is not a time in a valid format",
it: "non e' un formato ora valido"
},
/**
* Checks if {@param} <code class="param">input</code>'s value contains a string representing a time matching given {@param} <code class="param">timeFormat</code>
* @method assertTime
* @param input {DOM} the input to validate
* @param [timeFormat] {String} one among "hh:mm", "hh.mm", "hh:mm:ss" or "hh.mm.ss", default "hh:mm"
* @return {boolean} true if <code class="param">input</code>'s value is valid for given <code class="param">timeFormat</code>.
* @static
* */
assertTime: function(obj, timeFormat) {
var val = (obj.value || "").replace(/^\s+|\s+$/g, "");
if (val == "") {
return true;
}
var format = typeof(timeFormat) != "undefined" ? timeFormat : "hh:mm";
var supportedFormats = ["hh:mm", "hh.mm", "hh:mm:ss", "hh.mm.ss"];
var isSupported = false;
for (var i = 0; i < supportedFormats.length; i++) {
if (format.toLowerCase() == supportedFormats[i].toLowerCase()) {
isSupported = true;
break;
}
}
if (!isSupported) {
return false;
}
var separator = format.indexOf(":") != -1 ? ":" : ".";
var sp = val.split(separator);
var fsp = format.split(separator);
if (sp.length != fsp.length) {
return false;
}
var hh = sp[0];
var mm = sp[1];
var ss = null;
if (fsp.length == 3) {
ss = sp[2];
}
var check = function(what, max) {
if (what == null) {
return true;
}
else if (what.length != 2) {
return false;
}
else {
// workaround for parseInt bug (or feature?) for parseInt("0w") = 0
var re = /[0-9]/;
for (var i = 0; i < what.length; i++) {
if (!re.test(what.charAt(i))) {
return false;
}
}
what = parseInt(what);
if (isNaN(what) || what > max) {
return false;
}
}
return true;
};
return check(hh, 23) && check(mm, 59) && check(ss, 59);
}
};