locked
Checking The User Input RRS feed

  • Question

  • HI , I have a script in which i get user input using the following command
    $Version =Read-Host "Enter Version number in format x.x.x.x"

    Now , i need to check if user entered his input in form of x.x.x.x   
    Is there anyway i can check the user input   ,     and prompt user to enter again if its not in the format above.
    Tuesday, January 12, 2010 7:15 AM

Answers

  • Yes, it wil fail with that input - it requires exactly 4 decimal separated digits to match successfull.


    while(-not ($Verno -match '\d\.\d\.\d\.\d(\.d)*')) {$Verno =Read-Host "Enter Version number in format x.x.x.x"}

    will match 4 or more decimal separated, single digit version specifiers.  You will need a different regex to match less than 4, or specifiers with more than one digit between the dots.

    http://msdn.microsoft.com/en-us/library/hs600312.aspx

    • Marked as answer by Mervyn Zhang Wednesday, January 20, 2010 7:52 AM
    Tuesday, January 12, 2010 8:16 PM
  • as another example, the following regex will take 4 numbers or more, and each number can contain 1 or more digits

    while (-not ($version -match '\d+(\.\d+){3,}')) {$Version =Read-Host "Enter Version number in format x.x.x.x"}

    Greetings MOW 
    • Proposed as answer by John Whatever Tuesday, January 12, 2010 9:30 PM
    • Marked as answer by Mervyn Zhang Wednesday, January 20, 2010 7:51 AM
    Tuesday, January 12, 2010 8:46 PM

All replies

  • You can check this in PowerShell using the -Match operator, that takes a regex

    e.g.

    if ($version -match '\d\.\d\.\d\.\d') {'Version number OK'} else {'not OK'}

    you can also use this in a while loop for example :

    PS C:\Users\morsouw> while (-not ($version -match '\d\.\d\.\d\.\d')) {$Version =Read-Host "Enter Version number in format x.x.x.x"}

    Enter Version number in format x.x.x.x: 1
    Enter Version number in format x.x.x.x: 1.2.3.4

    Greetings MOW
    • Proposed as answer by Marco Shaw Tuesday, January 12, 2010 11:02 AM
    Tuesday, January 12, 2010 10:38 AM
  • Can you post your code?  MOW's method should work.
    Tuesday, January 12, 2010 7:09 PM
  • IT actually fails when ser enters 1.0.0.0.0  .   

    That is if you enter any version like x.x.x.x.x it fails


    $Verno=Read-Host "Enter Version Number of the Application (in the format x.x.x.x)  :"
    while (-not ($Verno -match '\d\.\d\.\d\.\d')) {$Verno =Read-Host "Enter Version number in format x.x.x.x"}
    Tuesday, January 12, 2010 8:06 PM
  • Yes, it wil fail with that input - it requires exactly 4 decimal separated digits to match successfull.


    while(-not ($Verno -match '\d\.\d\.\d\.\d(\.d)*')) {$Verno =Read-Host "Enter Version number in format x.x.x.x"}

    will match 4 or more decimal separated, single digit version specifiers.  You will need a different regex to match less than 4, or specifiers with more than one digit between the dots.

    http://msdn.microsoft.com/en-us/library/hs600312.aspx

    • Marked as answer by Mervyn Zhang Wednesday, January 20, 2010 7:52 AM
    Tuesday, January 12, 2010 8:16 PM
  • as another example, the following regex will take 4 numbers or more, and each number can contain 1 or more digits

    while (-not ($version -match '\d+(\.\d+){3,}')) {$Version =Read-Host "Enter Version number in format x.x.x.x"}

    Greetings MOW 
    • Proposed as answer by John Whatever Tuesday, January 12, 2010 9:30 PM
    • Marked as answer by Mervyn Zhang Wednesday, January 20, 2010 7:51 AM
    Tuesday, January 12, 2010 8:46 PM