Specifies that the text box can only enter a jquery code with numbers including decimals:
text boxes sometimes specify that only integers can be entered, here is not much to introduce, in particular, see How jquery specifies that a text box can only enter integers section, but sometimes you can enter decimals, and here's an example of how to do this with code examples.
The code example is as follows:
<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">//The text box can only enter numbers (including decimals) and mask input methods and pasteJQuery.fn.number=function(){ This. Bind ("KeyPress",function(e) {varCode=(E.keycode?E.keycode:e.which); //compatible with Firefox IE //You can't use the backspace bar under Firefox if(!$.browser.msie&&(E.keycode==0x8)){return;} if( This. Value.indexof (".")==-1){return(Code>= - &&Code<= $)||(Code== $);} Else{returnCode>= - &&Code<= $} }); This. Bind ("Paste",function(){return false;}); This. Bind ("KeyUp",function(){ if( This. Value.slice (0,1) == ".") { This. Value= ""; } }); This. Bind ("Blur",function(){ if( This. Value.slice (-1) == ".") { This. Value= This. Value.slice (0, This. Value.length-1); } }); }; $(function(){ $("#txt"). Number ();}); </Script></Head><Body><inputtype= "text"ID= "txt" /></Body></HTML>
The above code implements our requirements and allows you to enter numbers in a text box, including integers and decimals.
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=10801
For more information, refer to: http://www.softwhy.com/jquery/
Specifies that the text box can only enter a jquery code with numbers including decimals