Jquery controls only numbers with two decimal places.

Source: Internet
Author: User

Only JavascriptCode... But I can understand it...

/*

* Created by: Niu Yu
* Note: You can only enter numbers in the text box. The disadvantage is that you can enter Chinese characters. Therefore, you must make a judgment when the focus is lost.
*/
$ (Function (){
// Add a keypress to the text box, that is, judge when the keyboard is pressed
$ ("# Ctl00_contentplaceholder=txtsumvalue"). keypress (function (event ){
If (! $. Browser. Mozilla ){
If (event. keycode & (event. keycode < 48 | Event. keycode > 57) & event. keycode! = 46 ){
// IE6, 7,8, opera, and chrome
Event. preventdefault ();
}
} Else {
If (event. charcode & (event. charcode < 48 | Event. charcode > 57) & event. keycode! = 46) {
// firefox
event. preventdefault ();
}< BR >});

// When the text box loses focus, check whether the input is a number.
$ ("# Ctl00_contentplaceholder=txtsumvalue"). Blur (function (){
VaR input = $ (this );
VaR v = $. Trim (input. Val ());
// Alert ("input value:" + V );
VaR Reg = new Regexp ("^ [0-9] + (. [0-9] {2 })? $ "," G ");
If (! Reg. Test (V )){
Alert ("enter a number, which can only contain two decimal places! ");
Input. Val ("0 ");
}
});

});

If you use jquery, just import jquery.

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.