The following Javascript only allows a user to enter numbers in a field, and also limits the number of digits he/she can enter.
It works by constantly checking the field value via the setTimeout method every .05 seconds, removing non-numeric characters, and truncating the length if necessary.
This works in Notes R5 and above, INCLUDING in the client !
Remember to include the clearTimeout statement in the field's onBlur event, or the function will keep on running until the user exits the form.
Code
In the JSHeader
//Declare Global variables
var fName = ""
var nLen = 0
function limitField( f, n){
// Transfer local variables to globals
fName = f;
nLen = n;
limitLength();
}
function limitLength(){
if (fName != "" && nLen > 0){
with (document.forms[0].elements[fName]){
v = value
v = numsOnly(v)
if (v.length > nLen) {
value = v.substr(0,nLen)
}else{
value = v
}
timerID = setTimeout("limitLength()", 50);
}
}
}
function numsOnly (inString) {
// Removes all non-numeric chars
var s = inString
var nVals = "1234567890"; // acceptable chars; add + - . if floating values desired
var i;
var tmp = "";
// Search through string and append to unfiltered values to tmp.
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (nVals.indexOf(c) != -1){
tmp += c;
}
}
return tmp;
}
Example:
Field onFocus event
limitField(this.name, 10)
Field onBlur event
clearTimeout(timerID)