p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Visual Basic 2005 Basics (http://p2p.wrox.com/forumdisplay.php?f=137)
-   -   TextBox entry validation (http://p2p.wrox.com/showthread.php?t=65979)

thillai February 18th, 2008 10:18 AM

TextBox entry validation
Hello please help,
I am beginning to learn VB 2005 in my retirement as a hobby.
I have a small form with a textbox and OK button. I want to enter a date in the text box and on clicking OK the input has to be validated as a date. If invalid entry of date to be activated. I am unable to use do loop, as it turns out in a never ending loop. Please help with a bit of coding.

dparsons February 18th, 2008 10:25 AM

If this is a windows application you can use the MaskedTextBox to only allow for dates: http://msdn2.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask(VS.80).asp x

If this is a webform you can use a RegularExpressionValidator to verify that the data matches XYZ date pattern.


================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

zqureshi February 18th, 2008 10:41 AM

Hello Thillai

I'm also new to 2005 and .NET but I've just come across the ISdate function that returns a logical indicating if the given value can be converted to a Date.


Hope this is of Help

zqureshi February 18th, 2008 10:52 AM

Option Strict On
Public Class Form1

    Private Sub txtValidate_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles txtValidate.Click

        Dim DateOK As Boolean

        DateOK = IsDate(txtDate.Text)


    End Sub
End Class

thillai February 19th, 2008 01:25 PM

Hi all
Thanks for all the replies. But I may have not made it very clear. I am able to get the date and test it if it is valid date. If valid I am accepting it as a date of birth or whatever. If invalid date I want to be able go back and get the user to input again. This is where I have trouble. I coded a loop (Do While Loo) and it goes into an never ending loop.
How do I go back and allow reenter until a valid date has been entered. Please help, Thanks again to all.

BrianWren February 19th, 2008 02:26 PM

When validation fails, set the focus to the textbox, and exit the validating sub.
You can post a MessageBox first to alert the user as to why you are sending them back to the box.

It is not a very friendly thing to do to delete the contents when the contents are wrong, but of course you could. If that triggers a _Change event, and that's a problem, do this. Create a global boolean. At the top of the _Change event routine add If MyBoolean = True Then Exit Sub. In the validation routine, before deleting the contents, set MyBoolean to True. Empty the contents, then set the Boolean back to false.

You can sent the focus back to the cantrol, set the controls SelectionStart to 0 or 1 (I fergit), and set the SelectionLength to Textbox.Text.Length(). (The actual terms might be a little differetn, but this should at least convey the idea...)

thillai February 20th, 2008 07:23 AM

Hi all
Here I am again saying a big thank you to all of you who replied for my problem. I used a combination of MaskedTextBox and IsDate Validation and setting focus after failed validation for reentry and it works very well, better than my expectation!
Thanks again friends for all your help and looking forward to write to you all again when in difficulty.

All times are GMT -4. The time now is 07:59 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.