Several Methods for verifying that the TXT content is all numbers in Javascript

Source: Internet
Author: User

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 ~

 

 

 

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.