A price style is implemented based on the implementation of a text box that can only enter numbers and decimal points (two decimal places are reserved)

Source: Internet
Author: User

When I test the recharge interface, the input amount is retained to three decimal places when the program error, and later thought that the amount of money can only be retained to two digits after the decimal point, the following is the details of my implementation of jquery code, for reference only, the great God do not spray

1$(function() {2     //prevent key input other than the number key3$ ("#zf_money"). KeyDown (function(e) {4Digitinput ($ ( This), e);5     });6$ ("#zf_money"). KeyUp (function() {7         if($( This). val () = = "") {8$ ("#zfz_money"). Text ("0"));9}Else {Ten$ ("#zfz_money"). Text ($ ( This). Val ()); One         } A  -     }); -  the     functionPrevent (e) { -E.preventdefault?e.preventdefault (): e.returnvalue=false; -     }; -  +     functionDigitinput (el,e) { -         vare=e| | window.event;//FF, Chrome IE get event object +         varc=e.charcode| | E.keycode;//FF, Chrome ie get keyboard code A         varVal=el.val (); at         if(c==110| | c==190) {//110 (190)-Point on a small (primary) keyboard -(Val.indexof (".") >=0| |! Val.length) &&prevent (e);//An existing decimal point or text box is empty and the input point is not allowed -  -}Else if((c!=8&&c!=46&&//8-backspace, 46-delete -(c<37| | C&GT;40) &&//Notoginseng (max) (max)-left (UP) (right ) Arrow -(c<48| | c>57) &&//48~57-0~9 on the main keyboard in(c<96| | c>105))//96~105-0~9 of the keypad -|| E.shiftkey) {//Shift key, the corresponding code is toPrevent (e);//prevent events from propagating to KeyPress +}Else if(C!=8&&c!=46&&val.indexof (".") >=0) { -(Val.substring (Val.indexof (".") +1). Length) >1&&prevent (e); the}Else if(c!=8&&c!=46&&val.length>5) { * prevent (e); $         }Panax Notoginseng     }; -});

A price style is implemented based on the implementation of a text box that can only enter numbers and decimal points (two decimal places are reserved)

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.