How can I correctly format currency using jquery?

前端 未结 7 1869
故里飘歌
故里飘歌 2020-12-13 01:16

I do not need a mask, but I need something that will format currency(in all browsers) and not allow for any letters or special char\'s to be typed. Thanks for the help

7条回答
  •  情歌与酒
    2020-12-13 02:06

    As a corollary to why the jQuery FormatCurrency plugin is a good answer, I'd like to rebut your comment:

    1. code.google.com/p/jquery-formatcurrency - Does not filter out all letter's. You can type a single letter and it will not remove it.

    Yes, formatCurrency() by itself does not filter out letters:

    // only formats currency
    $(selector).formatCurrency();
    

    But toNumber(), included in the formatCurrency plugin, does.

    You thus want to do:

    // removes invalid characters, then formats currency
    $(selector).toNumber().formatCurrency();
    

提交回复
热议问题