Preface:
If Javascript is used to determine whether the TXT file is a number, check whether the input age is 0 ~ Between 100, the corresponding judgment has been made. However, when I want to use the same method to verify that all input numbers are numbers, I found a problem, such as the following:
Initial Practice:
First, set the environment, a TXT text input box, and a BTN submission, and trigger the verification that the text box is all numbers? SeeCode(JS snippets)
< Head runat = " Server " >
< Title > Verify that all input TXT content is numbers < / Title>
< Script Type = " Text/JavaScript " Language = " Javascript " >
// Description: shows how to verify that all contents in TXT are numbers.
// Copyright: http://www.cnblogs.com/yangmingming
// Notes: Apply a TXT Server Control and a BTN control.
Function Checktxt ()
{
VaR Elementtxt = Document. getelementbyid ( " Txtcheck " ). Value;
// Use the parseint function to extract numbers from strings (string type ).
If (Parseint (elementtxt) > 0 )
{
Alert ( " True " );
Return True ;
}
Else
{
Alert ( " False " );
Return False ;
}
}
< / SCRIPT>
< / Head>
True is returned when all input values are numbers. However, input values are similar'123abc', But there is an unexpected effect, see:
That is to say, this is the parseint function, which only truncates the numbers from it, and generates the result as shown in. What should we do?
Modification method:
It can be implemented in two ways: js-based judgment and regular expression-based judgment. Statement:
I) JS judgment:
Use js to judge, where the string method is used (which is not very familiar, huh, huh ). See the code first:
< Title > Verify that all input TXT content is numbers < / Title>
< Script Type = " Text/JavaScript " Language = " Javascript " >
// Description: shows how to verify that all contents in TXT are numbers.
// Copyright: http://www.cnblogs.com/yangmingming
// Notes: Apply a TXT Server Control and a BTN control.
Function Checktxt ()
{
VaR Elementtxt = Document. getelementbyid ( " Txtcheck " ). Value;
If (Checkdigit (elementtxt ))
{
Alert ( " True " );
Return True ;
}
Else
{
Alert ( " False " );
Return False ;
}
}
// Auxiliary Function to verify whether all strings of the input function are numbers
Function Checkdigit (STR)
{
VaR Letters = " 0123456789 " ;
For ( VaR I = 0 ; I < Str. length; I ++ )
{
If (Letters. indexof (STR [I]) =- 1 )
Return False ;
}
Return True ;
}
< / SCRIPT>
< / Head>
The debugging result is correct. Description:
That is, we get the correct result ~
Ii) Regular Expression judgment:
For more information about regular expressions, see the following code:
< Head runat = " Server " >
< Title > Verify that all input TXT content is numbers < / Title>
< Script Type = " Text/JavaScript " Language = " Javascript " >
// Description: shows how to verify that all contents in TXT are numbers.
// Copyright: http://www.cnblogs.com/yangmingming
// Notes: Apply a TXT Server Control and a BTN control.
Function Checktxt ()
{
VaR Regexp = / ^ \ D + (\. \ D + )? $ / ;
VaR Elementtxt = Document. getelementbyid ( " Txtcheck " ). Value;
If (Regexp. Test (elementtxt ))
{
Alert ( " True " );
Return True ;
}
Else
{
Alert ( " False " );
Return False ;
}
}
< / SCRIPT>
< / Head>
Through regular expression verification, the results are also judged by Js.
Summary: by verifying whether it is a number, we have further deepened our understanding of JS and will have the opportunity to further consolidate it in the future ~