JavaScript validation is a number

Source: Internet
Author: User
JavaScript limits the input of only numbers, determines the value of Event.keycode, and limits it to only numbers, if not numbers, returns an error, and if it is a number, continue.
Let me first show you how to verify that JavaScript is a number another way, by using the following functions: function Isnumber (onum)
{
if (!onum) return false;
var strp=/^/d+ (/./d+) $/;
if (!strp.test (Onum)) return false;
try{
if (parsefloat (onum)!=onum) return false;
}
catch (ex)
{
return false;
}
return true;
}
This JavaScript code is very concise, you can see. <script language=javascript>
function Onlynum ()
{
if (!) ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Consider the number keys on the keypad
Event.returnvalue=false;
}
</script>
<input onkeydown= "Onlynum ();" >
There is a more stupid way, we can also see, study
Next time, <script language= "JavaScript" with regular validation numbers >
function Checkmyform ()
{
var txt = myform.mytext.value;
if (CheckNumber (TXT))
{
Alert ("Only allow numbers to be entered.") ");
return false;
}
return true;
}
function CheckNumber (String)
{
var letters = "1234567890";
var i;
var C;
for (i = 0; i < string.length i + +)
{
c = String.charat (i);
if (Letters.indexof (c) ==-1)
{
return true;
}
}
return false;
}
</script>
<body>
<form name= "MyForm" onsubmit= "return Checkmyform ()" >
<input type= "text" name= "MyText" >
<input type= "Submit" Name= "Mysubmit" >
</form>
</body>
The simplest JavaScript validation is not numbers .The code is as follows: <input type= ' TEXT ' onkeypress= ' if (event.keycode==13) {if (isNaN (this.value)) alert (' Not numeric ')}; >
Using the Sinan function, click here to view the isNaN function description
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.