4 Replies - 1722 Views - Last Post: 13 November 2011 - 06:46 PM

#1 Yo!  Icon User is offline

  • D.I.C Head


Reputation: 4
  • View blog
  • Posts: 83
  • Joined: 03-September 11

Replace textbox(asp) character with another character?

Posted 12 November 2011 - 07:29 PM

In my application (yes I know that it's asp) I need to automatically replace a character if it is found in the textbox
Is there a way to do this.
If it helps this is how it renders on the page
<textarea id="ContentPlaceHolder1_TextBox3" cols="20" rows="2" name="ctl00$ContentPlaceHolder1$TextBox3"/>

Is there a way to call a function when the textbox's text is changed. I want to replace ("!") with (".").
Thanks in advance!
-Yo!

EDIT
if it helps here is the vb equivalent:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = TextBox1.Text.Replace("!", ".")
    End Sub


This post has been edited by Yo!: 12 November 2011 - 07:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Replace textbox(asp) character with another character?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Replace textbox(asp) character with another character?

Posted 12 November 2011 - 08:09 PM

I’d try
document.getElementById("ContentPlaceHolder1_TextBox3").addEventListener("input", function () {
    this.value = this.value.replace("!", ".");
}, true);

… in a decently modern browser

This post has been edited by Dormilich: 12 November 2011 - 08:10 PM

Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Replace textbox(asp) character with another character?

Posted 12 November 2011 - 08:30 PM

For compatibility with older browsers, you can use the change event instead. The simples way would be to:

  • Create function you want executed when the content changes:
    function onTextBoxChange( box ) {
        // This replaces all "!" with "."
        box.value = box.value.replace(/!/g, ".");
    }
    
    


  • Set the "onchange" property on the textarea in question to execute this function:
    <textarea onchange="onTextBoxChange(this)" ... />
    
    

This way; setting the "onchange" property on the element, rather than setting the event in Javascript, is the surest way to support older browsers. What Dormilich suggested, for example, would fail in all versions of Internet Explorer except 9 and later, as they don't support addEventListener. (Which is one great reason for using libraries like jQuery. They take care of all that while still allowing you to avoid putting Javascript into the HTML like this.)

Another issue is that this only executes once the input loses focus, not right away. You could set the keypress event as well to instantly swap out characters when typing into a box.
function onTextBoxKeyPress(box, evt) {
	if ( evt.charCode == 33 ) {
		evt.preventDefault();
		box.value += ".";
	}
}


<textarea onkeypress="onTextBoxKeyPress(this, event)" ... />


Was This Post Helpful? 1
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Replace textbox(asp) character with another character?

Posted 13 November 2011 - 02:17 AM

the IE equivalent (IE before version 9) to the input event would be
var area = document.getElementById("ContentPlaceHolder1_TextBox3");
area.attachEvent('onpropertychange', function() {
    area.value = area.value.replace("!", ".");
});

Was This Post Helpful? 0
  • +
  • -

#5 Yo!  Icon User is offline

  • D.I.C Head


Reputation: 4
  • View blog
  • Posts: 83
  • Joined: 03-September 11

Re: Replace textbox(asp) character with another character?

Posted 13 November 2011 - 06:46 PM

Thank you very much for all of your help!
You guys are life savers.
Yo!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1