473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Custom Date Validation Check Function

Hello, I have a VB.NET application with a Windows form that have
several textboxes fields where I have dates entered. I would like to
do a date validation check after the the field is updated, so I' using
the leave event.

Right now I am creating a 'leave' sub for each of the fields.
However, I'd like to simplify that and just call the name of a
function and plug the field name as a variable and be done.

In other words, I would like to be able to pass the field name as the
variable to a function that will handle the field's leave event and
then run the validation check. If the validation check fails, set the
focus back to the field name.
This is my original code:

Private Sub OrderDate_Leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OrderDate.Leave
' Code that check whether there's a valid date goes here.
End Sub

What I would like to have is:
'calling the validation date function from any part of my code
CheckforDateValidation(fieldname)

With a function that goes like this:

Public function CheckForDateValidation(ByVal fieldname As String)
That will have the event: Handles fieldname.Leave
' Code that check whether there's a valid date goes here.
' and if the validation check fails then
' set the focus back to the field that called the function.
fieldname.focus
End function
Does anyone knows how to accomplish this?

Thanks!

Mar 30 '07 #1
2 6718
On Mar 30, 9:47 am, "John Smith" <I...@NETZERO.NETwrote:
Does anyone knows how to accomplish this?

Thanks!
Using the Validating method would probably be better than the Leave as
you can set Cancel to true which will prevent the user from leaving
and you don't have to deal with any Focus code.
Make sure each Textbox has CausesValidation set to true then set up
the Handler similar to this:

Private Sub ValidateTextBoxes(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating,
TextBox2.Validating
' Cast the sender object to a Textbox
Dim txtSender As TextBox
txtSender = CType(sender, TextBox)
' If the date is not valid, set Cancel to true
e.Cancel = Not IsValidDate(txtSender.Text)
End Sub

Private Function IsValidDate(ByVal dateString As String) As
Boolean
' Perform date validation
End Function

You'll want to add each Textbox.Validating event to the Handles
statement or to make it look prettier you could do it manually in the
Form.Load with the AddHandler statement.

Mar 30 '07 #2
On Mar 30, 11:22 am, "jayeldee" <jayel...@gmail.comwrote:
On Mar 30, 9:47 am, "John Smith" <I...@NETZERO.NETwrote:
Does anyone knows how to accomplish this?
Thanks!

Using the Validating method would probably be better than the Leave as
you can set Cancel to true which will prevent the user from leaving
and you don't have to deal with any Focus code.
Make sure each Textbox has CausesValidation set to true then set up
the Handler similar to this:

Private Sub ValidateTextBoxes(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating,
TextBox2.Validating
' Cast the sender object to a Textbox
Dim txtSender As TextBox
txtSender = CType(sender, TextBox)
' If the date is not valid, set Cancel to true
e.Cancel = Not IsValidDate(txtSender.Text)
End Sub

Private Function IsValidDate(ByVal dateString As String) As
Boolean
' Perform date validation
End Function

You'll want to add each Textbox.Validating event to the Handles
statement or to make it look prettier you could do it manually in the
Form.Load with the AddHandler statement.
Hi Jayeldee, thanks for replying. I did as you suggested and it
worked. Thanks for all your help!

I really appreciate it.

Mar 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael ALbanese | last post by:
I am developing a telephone directory for my company. I have Fist Name, Last Name, Department and Phone Number as fields. A user can search by entering data into any one, or combination of these...
6
by: MickG | last post by:
Hi, I am trying to validate these values, this seems to work fine for the phone number and name but I am trying to get the program to fail to submit and set the focus on the date when 2006 is...
9
by: Alex Shirley | last post by:
Hi there I’m simply trying to check for a blank or empty value in a textbox on my webform. In this instance I do not want to use a requiredfieldvalidator, I want to use a customvalidator (as I...
3
by: Lisa Calla | last post by:
Hi, I've been struggling with this for a few days. I've seen bits and pieces (how to set up custom validation), but I can't seem to understand how to get client side validation for my custom...
6
by: jk | last post by:
Looking through WebUIValidation.js, I discovered that the standard validators don't cater for non-numeric date formats (e.g. dd-MMM-yyyy) which I would like to do To keep code to a minimum, I...
1
by: Elie Medeiros via .NET 247 | last post by:
Hi there, I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid...
2
by: daz_oldham | last post by:
Hi All I am looking to utilise the validation controls to validate a date of birth, however, I would like to take it one step further than just making sure that the user has entered a date. I...
8
by: Radu | last post by:
Hi. I have an ASP control on my page: <asp:Calendar ID="calStart" ................ Etc </asp:Calendar> and I have a Custom Validator defined as <asp:CustomValidator
2
by: tinariver | last post by:
I am using the onServerValidate of CutomValidator. I have got the Calender control and a text control. When user pick the date from calender conrol and value goes in the text box. Now I have to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.