0

Let's say I have a textboxA. When the application runs, textboxA will be shown and user can enter a number. I wanted to format the user input to red color font and enclose with brackets.

Example: If user enter '5', I will format the input to (5), with red color.

How do I achieve that?

sniggy
  • 151
  • 1
  • 3
  • 11
  • you want achieve that in client side or server side?? – Dolo May 22 '13 at 06:54
  • I wanted to achieve it at the client side. – sniggy May 22 '13 at 06:55
  • http://stackoverflow.com/a/3870087/706456, http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged.aspx, http://stackoverflow.com/a/6153068/706456 – oleksii May 22 '13 at 06:56

2 Answers2

0
function format(input) {

        var txtValue = document.getElementById(input).value;
        document.getElementById(input).value = "("+txtValue+ ")";
document.getElementById(input).style="color:red;";


    }

Use above javascript function and call in your textbox like this onchange="format(this);"

Dolo
  • 966
  • 14
  • 28
  • I've tried something like this but it doesn't work. Here's the code: private code txtBox_TextChanged(object sender, EventArgs e){ String txt = txtBox.Text; txtBox.Text = "(" + txt + ")"; txtBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000"); – sniggy May 22 '13 at 07:26
0

It worked for me-

Your textbox would be-

<asp:TextBox ID="TextBox1" runat="server" onchange="javascript:formatText(this);"></asp:TextBox>

On client side write a function as follows-

<script type="text/javascript" language="javascript">
    function formatText(input) {
        var txtValue = document.getElementById(input.id).value;
        document.getElementById(input.id).style = "color:Red;";
        document.getElementById(input.id).value = "(" + txtValue + ")";        
    }
</script>
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71