开发者

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 and LostFocus 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 (开发者_C百科i = 0; i < charactersToRemove.length; i++)
        value = value.replace(charactersToRemove[i], '');

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

I have tried to use ASPxFormatter but it is an internal class that is not indented to be used in a user project using String.Format('{0:' + displayFormat + '}', parseInt(value))); didn't work either, it threw an exception since String.format doesn't accept this format of pattern. Can you please provide a way to reformat my string to any pattern I want not only the one I illustrated? I highly appreciate your support....


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...

String.format = function() {
  var s = arguments[0];
  for (var i = 0; i < arguments.length - 1; i++) {       
    var reg = new RegExp("\\{" + i + "\\}", "gm");             
    s = s.replace(reg, arguments[i + 1]);
  }

  return s;
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜