PolarisPowerShellPSHTML

PSHTML tip to validate the input text field(s) against JSON file

I got an interesting requirement through a Facebook ping, I took some time to think through it and decided to blog after finding a nifty PSHTML tip. Here is the requirement “I need to validate the input text field against the JSON file which has huge list of names. Can I do it using switch, for loop, for-each loop? I need check name by hard-coding it right?”

Well, the requester has a valid point. Why to hard code the values in switch statements? Why to loop through in first place? It’s PowerShell! So, its always IT Pro friendly.

Here comes the nifty tip! #PSHTML and #Polaris module.


$Response.SetContentType('text/html')
$JsonContent = Get-Content -Path "C:\Temp\Users.json" | ConvertFrom-Json
$Html = form -action "/ValidUser" -method post -target _blank -Content {
input -type text -name text1 -Attributes @{pattern="$($JsonContent.Users.Name -join '|')"}
input -type submit -name submit1
}
$Response.Send($Html)

Enjoy PowerShell – We have more tips in the pipeline!

3 thoughts on “PSHTML tip to validate the input text field(s) against JSON file

  1. Hi Chen,

    I tried the above code but the output is not good.
    I get the output like below
    System.ArgumentException: Invalid object passed in, ‘:’ or ‘}’ expected. (105): { “Users”:[ { “Name”: “Hari” }, { “Name”:” “Ted” }, { “Name”: “PowerShell” } ] } at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at Microsoft.PowerShell.Commands.JsonObject.ConvertFromJson(String input, ErrorRecord& error) at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.ConvertFromJsonHelper(String input) at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.EndProcessing() at System.Management.Automation.CommandProcessorBase.Complete()At line:4 char:55 + … $JsonContent = Get-Content -Path “.\Users.json” | ConvertFrom-Json + ~~~~~~~~~~~~~~~~
    Please help.

  2. And also do I need to change the port number every time I run the script?
    Otherwise, it throws an error :
    Exception calling “Start” with “0” argument(s): “Failed to listen on prefix ‘http://localhost:8080/’ because it conflicts with an existing registration on the machine.”

Leave a Reply

Your e-mail address will not be published. Required fields are marked *