Jquery Input amount Format Limit plugin

Source: Internet
Author: User

(function($) {$.fn.extend ({money_mode:function(options) {varDefaults ={decimal_length:2,//Number of decimal placesFormat: "#,###.00",//format TypeLocale: "Us"            }; varOptions =$.extend (defaults, options); varLen = parseint (options[' decimal_length ')]); return  This. each (function() {                varInput = $ ( This); Input.css ("Ime-mode", "disabled"); varDecimal_point =false; Input.bind ("KeyPress",function() {                    if(Event.keycode = = 46) {                        if(Input.val (). IndexOf (".")!! =-1 | | (Input.val (). LastIndexOf (".") = = (Input.val (). length-1))) {                            return false; }                    } Else {                        if(Decimal_point)return false; Else                            returnEvent.keycode >= && Event.keycode <= 57;                }                }); Input.bind ("Blur",function() {    //begin string formatting after loss of focus                varFormatdata =Formatcodes (Options.locale.toLowerCase ()); varvalid =Formatdata.valid; varDec =Formatdata.dec; varGroup =Formatdata.group; varNeg =Formatdata.neg; if(Input.val ()! =NULL&& input.val ()! = ") {                        varText =NewString (jQuery (input). text ()); if(JQuery (Input). Is (": Input")) Text=NewString (jQuery (Input). val ()); varIsValid =true;  for(vari = 0; i < text.length; i++) {                            if(Valid.indexof (Text.charat (i)) = =-1) IsValid=false; }                        if(isValid) {varNumber =NewNumber (Text.replace (group, ")." Replace (Dec, "."). Replace (neg, "-")); varreturnstring = ""; varDecimalvalue = number% 1; if(Options.format.indexOf (".") >-1) {                                varDecimalportion =Dec; varDecimalFormat = options.format.substring (Options.format.lastIndexOf (".") + 1); varDecimalstring =NewString (decimalvalue.tofixed (decimalformat.length)); Decimalstring= Decimalstring.substring (Decimalstring.lastindexof (".") + 1);  for(vari = 0; i < decimalformat.length; i++) {                                    if(Decimalformat.charat (i) = = ' # ' && decimalstring.charat (i)! = ' 0 ') {decimalportion+=Decimalstring.charat (i);  Break; }                                    Else if(Decimalformat.charat (i) = = "0") {decimalportion+=Decimalstring.charat (i); }} returnstring+=Decimalportion}Else Number=Math.Round (number); varones =Math.floor (number); varOneportion = ""; if(ones = = 0) {oneportion= "0"; }                            Else {                                //find how many digits is in the group                                varOnesformat = ""; if(Options.format.indexOf (".") = =-1) Onesformat=Options.format; ElseOnesformat= options.format.substring (0, Options.format.indexOf ("."))); varOnetext =NewString (ones); varGrouplength = 9999; if(Onesformat.lastindexof (",")! =-1) Grouplength= Onesformat.length-onesformat.lastindexof (",")-1; varGroupCount = 0;  for(vari = onetext.length-1; i >-1; i--) {oneportion= Onetext.charat (i) +oneportion; GroupCount++; if(GroupCount = = Grouplength && I! = 0) {oneportion= Group +oneportion; GroupCount= 0; } }} returnstring= Oneportion +returnstring; if(Number < 0) returnstring+=neg; if(JQuery ( This). Is (": Input")) JQuery ( This). Val (returnstring); ElseJQuery ( This). Text (returnstring); }                    }     //end loses focus after string formatting}) Input.bind ("Paste",function() {                    vars = clipboarddata.getdata (' text '); if(!//D/.test (s));Value = S.replace (/^0*/, "); return false; }) Input.bind ("DragEnter",function() {                    return false; }) Input.bind ("KeyUp",function() {                    if(Input.val ()! =NULL&& input.val ()! = ") {                        if(Input.val (). IndexOf (".") = =-1) {                            return true; }                        Else {                            varDecimalindex = Input.val (). IndexOf ('. ')); varDecimalpart = Input.val (). substring (Decimalindex + 1, Input.val (). length); if(Decimalpart.length = =Len) {Decimal_point=true; return false; }                            Else                                return true; }                    }                    Else{decimal_point=false; return false;            }                });        });    }    }); functionformatcodes (locale) {//Default Values        varvalid = "1234567890.,-"; varDec = "."; varGroup = ","; varNeg = "-"; if(Locale = = "US" | | locale = = "cn" | | locale = = "TW") {Valid= "1234567890.,-"; Dec= "."; Group= ","; }        return NewFormatdata (valid, Dec, group, neg);    }; functionFormatdata (valid, Dec, group, neg) { This. valid =valid;  This. Dec =Dec;  This. Group =Group;  This. Neg =neg; };}) (JQuery); 

Jquery Input amount Format Limit plugin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.