Gyroscope / Validation Utilities
By default,
validators.js defines a few data types for client-side validation:
valstr - is it a non-empty string?
valemail - does the string conform to an email format?
valdate - enforces yyyy-mm-dd format
valfloat - is it a numeric type?
valrate - is the value a percentage? e.g. 5.2%
valint - is the value non-decimal numeric
valcurrency - enforces a monetary format of the current locale (v12.4)
The following coding pattern is recommended:
var otitle=gid('title');
var odate=gid('date');
if (!valstr(otitle)) return;
if (!valdate(odate)) return;
var title=encodeHTML(otitle.value);
var date=odate.value;
There are several subtle but important reasons why we'd want to store the
input field objects in temporary variables (the ones that begin with "o"). The
first reason is that a validation function needs access to the object, so that it
can change the background color of the input field to red if the value fails to
validate.
A second reason is to make the code more compact. Without the o-variables,
the code could look like this:
if (!valstr(gid('title))) return;
if (!valdate(gid('date'))) return;
var title=encodeHTML(gid('title').value);
var date=gid('date').value;
A third benefit of o-variables is that sometimes we need access to the
original, unescaped value for a tab title:
var oname=gid('oname');
if (!valstr(oname)) return;
var name=oname.value;
reloadtab('landlord_new', null, 'addlandlord&name='+name,
function(rq){
var llid=rq.responseText;
if (parseInt(llid,10)!=llid) return;
reloadtab('landlord_new', oname.value,
'showlandlord&llid='+llid,
null,null, {newkey:'landlord_'+llid}
);
}
);
Starting 12.4, valcurrency allows optional grouping symbols in the current locale. The grouping rules are defined in lang/dict.en.php
and lang/dict.en.js.
In English, both "1234.56" and "1,234.56" are accepted. However, in German, "1.234,56" and "1234,56" are accepted, not "1,234.56".
The server-side validators GETCUR and QETCUR should be used.
To display the value in the current locale, use
currency_format.