Text Box input restrictions

Source: Internet
Author: User

In the past, I often registered on some websites, but there are often some problems. For example, you may not easily fill in a lot of registration information, and then click Submit. A dialog box is displayed in the result, it is better to tell you what is wrong, or enter illegal characters, and then let you change the wrong one. Otherwise, you can write it again, another reason is that the user name has been registered, and then you have to enter the user name again and write some original information again. This is especially annoying, we can't do that.

I think the best way is to use the text change event during user input. If an error occurs, the input is immediately prohibited, it is annoying to wait until the user enters the correct information, so that the user can avoid having to go back and change a lot of things after writing the script. The following describes the input restrictions of several types of text. VB. NET is used here, but the idea is the same for other languages.

Only numbers can be entered (IDEA): All characters in the input text can be traversed and converted to single-precision floating point number single. If the conversion fails, it indicates that the input is not a number, capture error, and error prompt, the Code is as follows:

Public Class TextBoxManagement Function OnlyIntNumber (ByVal Content As String) Try Dim textContent As String textContent = Convert. toSingle (Content) 'convert to single-precision floating point Catch ex As Exception MsgBox ("only numbers allowed") 'If the conversion fails, Skip here End Try Return 0 End FunctionEnd Class

Only Chinese characters can be entered: Asc Function

Dim TextBoxContent As String = Asc ("Chinese character") If TextBoxContent <0 Then MsgBox ("Chinese character") End If

It is worth noting that Asc converts characters into ASCII codes. We can query the ASCII table. If the characters in the ASCII code written in your function are "a, B, and c ", the converted ASCII code is the corresponding ASCII code of the character, but if it is a Chinese character, it is wrong to say that the result is an ASCII code of the Chinese character, because there is no Chinese Character in the ASCII code, in addition, this function is converted to a byte character while the Chinese character is two bytes. Therefore, the conversion is not an ASCII code, but a negative number, I don't know how this result can be obtained through Internet queries, But I can use the xx algorithm to convert it into Chinese character encoding. I will not go into details here, the conversion result is a negative number to determine whether the character is a Chinese character, but it is not absolute. If the character to be converted is a character encoded in more than 1 byte, then we will get a negative number, so you just need to know this.

Others: You can use the Asc function to determine the range of ASCII codes from the ASCII table based on your needs. In this way, you can restrict the single-byte characters you enter! No, it's strange. In fact, we can still use ASCII to determine the number. Well, let's go back and try it!

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.