Allow only one number and one decimal point in the HTML text input box

Source: Internet
Author: User

Restrict the user to enter invalid numbers and decimal places, leading to poor price calculation.CodeFor more information, see.

 <  Html  >  
< Head >
< Title > Text number </ Title >
< Meta HTTP-equiv = "Content-Type" Content = "Text/html; charset = GBK" >
</ Head >
< Script Type = "Text/JavaScript" >
Function Check (event ){
VaR E = Window. Event | Event;
VaR Target = E. srcelement | E.tar get;
VaR K = E. keycode;
If (Isfunkey (k )){
Return True ;
}
VaR C = Getchar (k );
If (Target. value. Length = '' && (C = ' - ' | C = ' + ' )){
Return True ;
}
If (Isnan (target. Value + Getchar (k ))){
Return False ;
}
Return True ;
}
Function Isfunkey (CODE ){
// 8 --> backspace
// 35 --> end
// 36 --> Home
// 37 --> left arrow
// 39 --> right arrow
// 46 --> Delete
// 112 ~ 123 --> F1 ~ F12
VaR Funkeys = [ 8 , 35 , 36 , 37 , 39 , 46 ];
For ( VaR I = 112 ; I <= 123 ; I ++ ){
Funkeys. Push (I );
}
For ( VaR I = 0 ; I < Funkeys. length; I ++ ){
If (Funkeys [I] = Code ){
Return True ;
}
}
Return False ;
}
Function Getchar (k ){
If (K > = 48 && K <= 57 ){
Return String. fromcharcode (k );
}
If (K > = 96 && K <= 105 ){
Return String. fromcharcode (K - 48 );
}
If (K = 110 | K = 190 | K = 188 ){
Return " . " ;
}
If (K = 109 | K = 189 ){
Return " - " ;
}
If (K = 107 | K = 187 ){
Return " + " ;
}
Return " # " ;
}
</ Script >
< Style Type = "Text/CSS" >
Textarea {
Width : 200px ;
Height : 80px ;
}
Span {
Color : # Ff0000 ;
}
</ Style >
< Body >
< Input Style = "Ime-mode: Disabled ;" Onpaste = "Return false" Onkeydown = "Return check (event )" Onkeyup = "If (isnan (this. Value) | this. value <0 | this. value> 59) This. value = '0 '" >
</ Body >
</ Html >

Text number

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.