The Kirkland Coder: December 2014

Tuesday, December 9, 2014

When I need to allow only Net Bios characters I use this function. It can be used to make sure a user enters a proper server name.

/// 
/// Will allow text entered into a textbox to be Net Bios safe.
/// If the key pressed is not good, it is fake handled. 
/// 
/// Can be called from XAML like so:
///     
///     
/// NOTE: PreviewTextInput is WPF Device safe as compared to checking the keyboard.
/// 
/// 
/// 
public static void NetBiosAllowedTextOnly(object sender, TextCompositionEventArgs e)
{
    // Get the entered text
    string TheText = e.Text;

    ////////////////////////////////////////////////////////////////////////////
    //    NetBIOS computer names cannot contain the following characters:     //
    //    • backslash (\)           • slash mark (/)                          //
    //    • colon (:)               • asterisk (*)                            //
    //    • question mark (?)       • quotation mark (")                      //
    //    • less than sign (<)      • greater than sign (>)                   //
    //    • vertical bar (|)                                                  //
    ////////////////////////////////////////////////////////////////////////////
    if (-1 < TheText.IndexOfAny(new char[] { '\\', '/', ':', '*', '?', '\"', '<', '>', '|' }))
    {
        // Not allowed character so pretend it was handled.
        e.Handled = true;
    }
    else
    {
        // Allowed character, let it be handled regularly.
        e.Handled = false;
    }
}