Input Validation / Masked Text Box for Validation
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Another technique that can be used to help in the input validation of your projects is to use the mask text box control, which is similar to the text box control that we've already become comfortable with, but has some other features associated with it. The text box, of course, is drawn from the toolbox, just as it normally has and can be. The mask text box, which has a slightly different icon associated with it can also be drawn on the form in the same manner and in fact, at design time they look virtually identical and of course can be resized to any necessary size. The difference will become evident in the mask text box when you experiment with the properties window. The mask property for the mask text box control has an ellipses button that, when pressed, will provide you the input mask dialog box that provides the various choices of the predefined mask descriptions that are built into the Visual Studio environment. By the way, you can actually create your own custom input mask, but chances are the ones that are already provided will solve the problem and aide in the data validation process for your projects. As you can see, there is one for numeric, there is a phone number with and without the area code, two different forms of the date, one including the time, a social security number field that includes the dashes in between the nine digits, two different forms of the time; one in European and military time as well as the US time and there is also a zip code that includes the four-digit extension. And of course, there is the custom one that you can create as well. At the bottom of the form you'll notice there is a mask in a preview window or preview box that gives you a view of what they will look like on the form once you implement it. There's also a check box that allows you to turn on or turn off the Use Validating Type Feature. The validating type will check to make sure the data being entered is conforming to the proper default data type. Notice some of the input masks, such as the phone numbers, do not have a validating type. That is, they're not going to be looking for specific data, but simply the number of characters and the proper form and will aide in the process of entering the area code, for example, in the parenthesis and inserting the dashes in the appropriate places and so forth. Using the numeric five digits example, I'll click OK and as you can see on the form, I now have a text box that will prompt the user to enter five digits into the text box. I am attempting at this moment to try to enter more than five digits. It will not let me, since that's all that it's been designed to accept. Returning to the input mask dialog box, let's take a look at the phone number and again, at run time you will see that the user is able to enter just simply digits. They do not need to enter any punctuation, such as parenthesis or dashes. The data is being entered and then is being formatted according to the input mask. Let's take one more look at another example. The social security number; very useful. When executed, once again, all the user needs to do is to type in data and the hyphens take care of themselves. Now, you notice that the default character is the underline. This can be changed. If, for example, using the social security number as my demonstration, if I go to the property called Prompt Character and type a hyphen, for example, or I could use other characters if I choose, instead of the default underscore, I now, when I run the program, I am now will see that when the user enters data, they are provided the pound symbols or the number symbols is their prompts, giving them the indication of the format that's being desired. So the input mask can be very useful in helping in the validation of data. It is not looking for specific data in most cases. It is, however, prompting the user and inserting appropriate characters. That is, at run time the user can not enter characters that do not conform to that specific mask. For example, the phone number and social security number masks do not allow input other than numeric digits. One last note; for a date or time mask, the user can enter only numeric digits but may possibly enter an invalid number. For example, a month may be greater than 12. The mask will accept any numeric digits which could possibly call as your program to generate a run time error. So in those type situations, you will still need to use If statements and other input validation techniques that have been discussed to make sure the data entered is of valid data type and does not exceed a specified range of values.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 