none
Finding specified bad characters within a wpf textbox using Powershell. RRS feed

  • Question

  • Hey guys,

    Need some help trying to figure this out. I am trying to only allow certain characters to be entered within my wpf textboxes. The characters that should be allowed are: '0-9' and '.'. I have created an array of bad characters and am not trying to apply some sort of array comparison when text is entered. 

    I cant however seem to get it to work. 

    CODE:

    Creating Arrays (Note Bad Character Array):

    

    Code applied to WPF Textbox:

    Any Help would be greatly appreciated. 

    Thanks 

    Jay

    Thursday, January 12, 2017 7:54 AM

Answers

  • Hi Jay,

    you can do this using regex:

    if ($Quotas_Issuewarning_TB.text -notmatch "^[\d.]+$")
    {
    "error"
    }
    else
    {
    "all is well"
    }

    Also, note that this will still allow the users to use multiple dots, which may be ... undesirable. To enforce only single dot, use this:

    if ($Quotas_Issuewarning_TB.text -notmatch "^\d+$|^\d+\.\d+$")
    {
    "error"
    }
    else
    {
    "all is well"
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Josh.Ads Thursday, January 12, 2017 8:47 AM
    Thursday, January 12, 2017 8:17 AM

All replies

  • Hi Jay,

    you can do this using regex:

    if ($Quotas_Issuewarning_TB.text -notmatch "^[\d.]+$")
    {
    "error"
    }
    else
    {
    "all is well"
    }

    Also, note that this will still allow the users to use multiple dots, which may be ... undesirable. To enforce only single dot, use this:

    if ($Quotas_Issuewarning_TB.text -notmatch "^\d+$|^\d+\.\d+$")
    {
    "error"
    }
    else
    {
    "all is well"
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Josh.Ads Thursday, January 12, 2017 8:47 AM
    Thursday, January 12, 2017 8:17 AM
  • Argh! Regex! 

    Thanks heaps for this, works great. 

    Thursday, January 12, 2017 8:48 AM