regex to validate the date DD/MM/YYYY format

Sagar0650

Board Regular
Joined
Nov 25, 2019
Messages
55
Office Version
  1. 2019
Platform
  1. Windows
i want to validate date taken from textbox(excel forms) using regular expression & if it is wrong it should popup msg to enter correct date format
date format is DD/MM/YYYY
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Do you really need to use a regular expression?

Couldn't you use IsDate to check a valid date has been entered and if so format it as required?
 
Upvote 0
i am not sure if that will work.
if you can help me with isdate, it will be great
Thank you
 
Upvote 0
Something like this perhaps
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    With Me.TextBox1
         If IsDate(.Value) Then
             .Value = Format(DateValue(.Value, "dd/mm/yyyy")
         Else
             MsgBox "Please enter a valid date.", vbInformation,  "Invalid date"
             Cancel = True
        End If
    End With

End Sub
 
Upvote 0
when i am clicking in the textbox, i am getting a function as private sub textbox1_change()
you have mentioned as textbox1_exit()
sorry for bothering you again
i am kind of new to vba
 
Upvote 0
I'm assuming the textbox is on a userform.

If it is when you double click it you will see the Change event but you should also be able to select it's other events e.g. Exit from the right hand dropdown above the code window.
 
Upvote 0
yes the textbox is on userform
If it is when you double click it you will see the Change event but you should also be able to select it's other events e.g. Exit from the right hand dropdown above the code window.
you want me to change any property of textbox from the property window?
 
Upvote 0
No, but it would be good to know where the textbox is located.

Is it on a userform or a worksheet?
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top