Tuesday, November 30, 2010

Get Amicable Safety Figures Right

A familiar growth use box is to guide users whenworking with submit fields that need a definite submit format. For example,credit card and amicable safety number fields use disposition delimiters that youmay wish to make on a field. The subsequent to representation uses JavaScript to increase adefined delimiter disposition according to a tangible character.

The American amicable safety pattern is tangible as xxx-xx-xxxx.Users that sort 123456789 should have the submit automatically prepared to123-45-6789 whilst they type. Also, the margin should be stable from characterinput and submit length incomparable than the supposing pattern.

;
;
;

With the on top of configuration, the handleNumberFormatConversion way is called for any key strokein the submit field. Additional arguments supposing to the function are the inputpattern and the disposition delimiter.

The JavaScript ethics that is addressed by the clientListeneron the InputText is shown below:

//JavaScript function that relates a definite format
// to numeric input. Thepattern evidence defines the
// submit mask, for instance xxx-xx-xxxx. The delimiter defines
// the delimiter disposition to increase to the user submit formed
// on the pattern

functionhandleNumberFormatConversion(pattern, delimiter){
lapse function(evt){
var inputField = evt.getCurrentTarget();
var keyPressed = evt.getKeyCode();
var oldValue = inputField.getSubmittedValue();
//keycode 48-57 are keys 0-9
//keycode 96-105 are numbpad keys0-9

var validKeys = newArray(48,49,50,51,52,53,54,55,
56,57,96,97,98,99,100,
101,102,103,104,105,
AdfKeyStroke.ARROWRIGHT_KEY,
AdfKeyStroke.ARROWLEFT_KEY,
AdfKeyStroke.BACKSPACE_KEY,
AdfKeyStroke.DELETE_KEY,
AdfKeyStroke.END_KEY,
AdfKeyStroke.ESC_KEY,
AdfKeyStroke.TAB_KEY);

varnumberKeys = new Array(48,49,50,51,52,53,54,55,
56,57,96,97,98,99,100,
101,102,103,104,105);

var isValidKey = false;
for (var i=0; i <validKeys.length; ++i){
if (validKeys[i] == keyPressed){
isValidKey = true;
break;
}
}
if(isValidKey){
//key is valid, ensureformatting is correct
var isNumberKey = false;
for (var n=0; n <numberKeys.length; ++n){
if(numberKeys[n] ==keyPressed){
isNumberKey = true;
break;
}
}
if(isNumberKey){
//if the user supposing enoughdata, call off
//the input
var formatLength =pattern.length;
if(formatLength ==oldValue.length){
inputField.setValue(oldValue);
evt.cancel();
}
//more values allowed. Checkif delimiter needs to
//be set
else{
//if the date format has adelimiter as the next
//character, increase it
if(pattern.charAt(oldValue.length)==delimiter){
oldValue =oldValue+delimiter;
inputField.setValue(oldValue);
}
}
}
}
else{
//key is not valid, so undoentry
inputField.setValue(oldValue);
evt.cancel();
}
}
}

The representation is for number usually input. However, varying itfor disposition or churned submit is not tough to do. Note however that youcan't use this with af:inputDate part since this part doesn't workwell when surroundings String formatted values as the worth property. (At smallest my debuggingshowed this)

No comments:

Post a Comment