Verified if the number is a valid year

Hello group, it’s been a while since I wrote. I would like to check if an entered number and a valid date, I wrote this code. Do you think it can go? or can it be improved?
As a valid year range I have given from 1900 to the current year.

Dim ReadYear as integer
Dim InitYear as integer
var CurrentYear as datetime=datetime.Now

if IsNumeric(TextField1.Text)=True then ' Is is Numeric i verified if is date
  ReadYear=integer.FromString(textField1.Text)
  if ReadYear<=1900 or ReadYear>=(CurrentYear.Year+1) then
    msgbox " Attenzione, si accettano date superiori al 1900 e minori o uguali all'anno in corso"
  else
    if MsgBox("Eliminare tutti i dati in magazzino dell'anno  " + TextField1.Text , 36)=6 then
      msgbox " ELIMINO"
    end if
  end if
else
  msgbox " Attenzione, inserire un anno valido"
end if

I believe that code would validate an entry of “1976.34”.

You can check the Length of the TextField, or you can use a regular expression.

if IsNumeric(TextField1.Text) and TextField1.Text.Length = 4 then ...

or

var rx as new RegEx
rx.SearchPattern = "^\d{4}$"
if rx.Search(TextField1.Text) isa RegExMatch then ...

Thanks it’s perfect.

1 Like