简体   繁体   中英

Format a string using a string pattern

I have a displayFormat pattern "$###,###,###;-$###,###,###;#" (it can be different too) and I want to reformat the value in the AspxTextbox after deleting the ',' on GotFocus<\/code> and LostFocus<\/code> events by calling the following JavaScript function :

function TextBoxFormat(ctrl, e, displayFormat, charactersToRemove) {
var value = ctrl.GetValue();
var i;

if (value != null && charactersToRemove != null) {
    for (i = 0; i < charactersToRemove.length; i++)
        value = value.replace(charactersToRemove[i], '');

    ctrl.SetValue(ASPxFormatter.Format('{0:' + displayFormat + '}', 
         parseInt(value)));
}

The MaskedEdit in the ajax control toolkit looks very much like what you want to do. If you don't want to use the pre-built controls, you can get the javascript source in one of the download packages.

Here is the format function...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM