forum

Home / DeveloperSection / Forums / Insert space between numbers in Textbox

Insert space between numbers in Textbox

Anonymous User 1937 08-Dec-2014

I have this TextBox where a user can type in his/her Phone number.

 

<asp:TextBox runat="server" ID="txtPhone" CssClass="TicketField FieldWidthH txtboxStyle" />

If the user types 12345678 I would like it to auto "show" 12 34 56 78 so there is a space after each 2 numbers. 

Here is my C# code: 

if (IsPostBack) {
    if (!string.IsNullOrEmpty(txtPhone.Text)) {
        // Check if the value contains any non-numeric values and strip them out
        string cleanedUpTextBoxValue = Regex.Replace(txtPhone.Text, @"^\d{6}$", "");
 
         // Parse your number as an integer from your TextBox (after cleaning it up)
         Int64 yourIntegerTextBoxValue = Int64.Parse(cleanedUpTextBoxValue);
 
         // Format your number as a string
         string formattedResult = yourIntegerTextBoxValue.ToString("## ## ## ##");
 
         // Output the result into your TextBox
         txtPhone.Text = formattedResult;
    }
}
I also tried string cleanedUpTextBoxValue = Regex.Replace(txtPhone.Text, "[^\d]", "");

But then I type in the Textbox I still only displays the numbers as 12345678.

What am i doing wrong?

Thanks for your time


Updated on 08-Dec-2014
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By