JavaScript fun: is a string composed of letters or numbers?

Source: Internet
Author: User
Sometimes you need to verify whether an input string is composed of letters or numbers. An English word exactly interprets this requirement-alphanumeric. Sometimes you need to verify whether an input string is composed of letters or numbers. An English word exactly interprets this requirement-alphanumeric.

Further refine this requirement:

1. The string consists of at least one character (that is, the Null String is not passed)

2. uppercase/lowercase English letters and 0-9 digits are allowed (this is the main character)

3. Other characters, as long as they appear, cannot exceed, such as spaces and underscores.

For this question, regular expressions are just a single sentence, but what if regular expressions are not used?

It is also very simple. You can use ASCII codes to traverse strings one by one to determine.

Let's take a look at the ASCII code range:

1. digit characters 48-57

2. uppercase letters 65-90

3. lowercase letter 97-122

All the characters in these ranges are valid characters, so those out of the range must be invalid characters.

But don't forget the special case mentioned above-a null string.

Well, with these materials, we can easily write them out.

Function alphanumeric (string) {if (string. length <1) {return false ;}for (var I = 0; I

 
  
57 & code <65 | code> 90 & code <97 | code> 122) {return false ;}} return true ;}

 

The above is JavaScript fun: is a string composed of letters or numbers? For more information, see the PHP Chinese website (www.php1.cn )!

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.