Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 95
Reply with quote  #1 

I wrote the code below that prompts the user to enter a start date.  This should be required and it should be in the correct date format yyyy-mm-dd.  I used the While statement to make the $inputDate required but I need to check that the date is in the format.  I think I need an use an if statement to check if $inputDate matches $dateFormat, but not sure where it goes with While.  If it $inputDate does not match it should keep prompting user until it is correct.

$dateFormat = '\d\d\d\d-\d\d-\d\d'

$inputDate = Read-Host -Prompt "Enter Start Date for new account [yyyy-mm-dd][Required]"

while (!$inputDate) {

 Write-Host "ERROR: Start Date is Required."

 $inputDate = Read-Host -Prompt "Enter Start Date"

 Write-Host "Input read: $inputDate"  }




0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 375
Reply with quote  #2 
Hi,

if you can live without the error message, you'd get by with


$regex = "\d{4}-\d{2}-\d{2}"
do {
    $date = Read-Host -Prompt "Enter Start Date for new account [yyyy-mm-dd][Required]"
} until ($date -match $regex)


The far bigger problem with this is that this does not check for a valid date but only for four, two and two digits with dashes in between. It would accept 0000-99-99 as well as 2017-02-29, neither of which is a valid date.

To tackle this, you need to either craft a regex that only matches valid dates, which will result in something like

^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

(stolen from https://stackoverflow.com/questions/51224/regular-expression-to-match-valid-dates/8768241#8768241)
or, which what I'd do, check if the date indeed can be cast as a [datetime] value:

$regex = "\d{4}-\d{2}-\d{2}"
do {
    $date = Read-Host -Prompt "Enter Start Date for new account [yyyy-mm-dd][Required]"
    if ($date -match $regex) {$datecast = $date -as [datetime]}
} until ($datecast)

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 375
Reply with quote  #3 
Addition: I would always define my script or function parameters as advanced parameters, including validation, rather than querying the user at the console. That kinda destroys that early 1980s user experience... but well, I personally don't miss that, even if I do miss the times :-)

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 95
Reply with quote  #4 
When I run the code, the $datecast output is include the time.  Also, the date is not yyyy-mm-dd 


  Capture1.png 

0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 375
Reply with quote  #5 
I never said you have to actually use $datecast in your further processing. But, if $datecast contains a value, it indicates that $date contains a valid date. And since we also do a regex match, $date will have the expected format.

Regarding „$datecast contains the time“: The [datetime] object type always has the time part, and what you see on the console is not the data itself but it’s default representation as text. You could chose a representation not containing hours and minutes, or the weekday, for that matter.

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.