Set text in JS + CSS to enter only numbers

Source: Internet
Author: User
<Script language = "JavaScript">

<! --

VaR isie = false;

VaR isff = false;

VaR Issa = false;


If (navigator. useragent. indexof ("MSIE")> 0) & (parseint (navigator. appversion)> = 4) isie = true;

If (navigator. useragent. indexof ("Firefox")> 0) isff = true;

If (navigator. useragent. indexof ("safari")> 0) Issa = true;


Function onlynumber (E)
{

Var key;

Ikeycode = Window. event? E. keycode: E. Which;

If (! (Ikeycode> = 48) & (ikeycode <= 57) | (ikeycode = 13) | (ikeycode = 46) | (ikeycode = 45) | (ikeycode = 37) | (ikeycode = 39) | (ikeycode = 8 )))

{

If (isie)
{

E. returnvalue = false;

}
Else
{

E. preventdefault ();

}
}
}

// -->

</SCRIPT>

<Input type = "text" onkeypress = "Return onlynumber (event)">


Another one:

==================================

<Input type = "text" onkeyup = "This. value = This. value. Replace (/[^ \. \ D]/g ,'');
If (this. value. Split ('.'). length> 2 ){
This. value = This. value. Split ('.') [0] + '.' + this. value. Split ('.') [1]} ">
<% = Text_field_tag "close_sight", 4,: onkeypress => "Return onlynumber (event);",: Style => "ime-mode: Disabled" %>

// Positive integer
Function onlynum (OBJ ){
Console. Log (obj. value );
OBJ. value = obj. value. Replace (/[^ \ D]/GI ,"");
}

// Positive and negative integers
Function numhasplus (OBJ ){
// Take the first digit of someone else
VaR T = obj. value. charat (O );

// Replace the part that is not a number
VaR _ value = obj. value. Replace (/[^ \ D]/GI ,"");

// If it is a negative number
If (t = '-'){
_ Value = '-' + _ value;
}

// Page
If (obj. value = _ value)
Return;
OBJ. value = _ value;
}

// Decimal
Function onlyfloat (OBJ ){
// Obj. value = obj. value. Replace (/[^ (? \ D +) (\. \ D +)?] /GI ,"");
VaR RegEx =/^ [+ |-]? \ D *\.? \ D * $ /;
If (RegEx. Match (obj. Value )){
Return;
}
VaR o_value = obj. value. Replace (/[^ (? \ D +) (\. \ D +)?] /GI ,"");
If (o_value = ''| o_value = NULL ){
OBJ. value = 0;
Return;
}
O_value = o_value.tostring ();
VaR o_array = o_value.split ('.');
VaR m_value = 0.0;
If (o_array.size () <= 1 ){
M_value = parsefloat (o_array [0]);
}
Else {
M_value = parsefloat (NAN (o_array [0] + '.' + o_array [1]);
}
If (obj. value = m_value)
Return;
OBJ. value = m_value;
}



<INPUT CLASS="textbox" TYPE="text" STYLE="ime-mode:disabled">
No matter what input method you use when you come in, all input is in English.
IME-mode Syntax:
IME-mode: auto | active | inactive | disabled; Value: Auto: default value. The IME status is not affected. It is the same as when the ime-mode attribute is not specified. Active: All characters entered using IME are specified. Activate the local language input method. You can still deactivate imeinactive by specifying all characters that do not use ime. Activate a non-local language. You can still deactivate imedisabled: Disable IME completely. For controls with focus (such as input boxes), you cannot activate ime.
Text-transform Syntax:
 
Text-transform: None | capitalize | uppercase | lowercase; Value: None: default value. Capitalize is not converted: converts the first letter of each word to uppercase, and the rest to uppercase. uppercase: converts to uppercase. lowercase: converts to lowercase.
Related Article

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.