Forum Moderators: open

Message Too Old, No Replies

Change Text on Textbox

Input

         

umerkk

7:41 pm on May 31, 2007 (gmt 0)

10+ Year Member



Hi

I want a peice of javascript that whenever quotes "" are typed on textbox it automatically convert them into single quotes '' or warn user that this text is not allowed to type..

Anyone, Javascript or PHP?

StupidScript

10:54 pm on May 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<script type="text/javascript">
document.onkeypress = keyHit;
var keyName;
function keyHit(evt) {
var e = evt ¦¦ window.event;
var thisKey = e.which ¦¦ e.keyCode;
keyName = String.fromCharCode(thisKey);
}
function chkForm() {
if (keyName == '"') {
alert("You are not allowed to use quotation marks.\nPlease use single-quotes, instead.");
txtFld = document.getElementById("txt");
txt = txtFld.value.substring(0,(txtFld.value.length-1));
txtFld.value = txt+"'";
}
}
</script>
<form>
<textarea id="txt" cols=15 rows=5 onkeyup="chkForm()"></textarea>
</form>

(The alert could be really irritating, and therefore is optional. :) )

If you will be parsing the form output using PHP, and are trying to get rid of the quotation marks for, say, entering the data into a database or something, you don't need to worry about what they type. When the form is submitted, you could use this PHP:

$textareaValue=htmlspecialchars($_POST['textarea'],ENT_QUOTES,'UTF-8');

or

$textareaValue=str_replace('"','\'',$_POST['textarea']);

[edited by: StupidScript at 10:58 pm (utc) on May 31, 2007]

Dabrowski

11:05 pm on May 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this, it's a simple function that swaps the quotes when the box loses focus.

Note, when you change the text, if the box has focus the cursor will be moved to the end of the text, so be careful when doing it. This is why I do it when the box loses focus.

First you need to give your text box an ID so we can find it later, and give it an event so we can check the quote marks:

<input id='mytextbox' .... onBlur='swapQuotes();'>

onBlur is the event used when the user clicks or tabs off the box.

Now the function, we'll use a simple replace function to do all the characters in one go. Just in case you don't know, this should go in your <head> as shown...


<head>
...
<script>
function swapQuotes( evt) {
var e = evt ¦¦ event;
var elm = e.target ¦¦ e.srcElement;

elm.value = elm.value.replace( /"/g, "'");
}
</script>
</head>

Try that.

umerkk

9:14 pm on Jun 1, 2007 (gmt 0)

10+ Year Member



Both Did Not Work

Bernard Marx

10:30 pm on Jun 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not a very helpful reply after all the hard work.
In what way did they not work?

Note that the ¦ character displayed in WebmasterWorld posts must alway be replaced with an unbroken pipe character.

(Long time, no see, Mr SS)

[edited by: Bernard_Marx at 10:30 pm (utc) on June 1, 2007]

StupidScript

1:36 am on Jun 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like Dabrowski's approach ... why bother freaking out the visitor with quotes-that-change-to-apostrophes as they type? Just replace them all when they're done! Nice.

I dunno why they wouldn't work for you, umerkk. Are you able to try both the Javascript and the PHP solutions? Any of them should do the job.

Hey, Mr. Marx! Yeah ... been bopping around. It got frustrating when my codes were being constantly improved upon by your superior skills. (D'oh! ;) I know ... ass-whuppin' time ...) Kidding! I'm glad you're still around, too!

<edit>
BTW, what's the deal with the pipe character? Why change it, when you can neuter it, and which character on the keyboard is the solid pipe, anyway? Often wondered, never checked it out ...
</edit>

[edited by: StupidScript at 1:39 am (utc) on June 2, 2007]

umerkk

7:00 pm on Jun 2, 2007 (gmt 0)

10+ Year Member



Php Code Helped Me Out, I was Just Saying that Javascript Doesnt Work, I Appreciate all your help

Dabrowski

7:16 pm on Jun 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That JavaScript does work. I tested it on my development server.

The most likely cause is, as he said, the misrepresentation of the pipe character.

umerkk

7:32 pm on Jun 2, 2007 (gmt 0)

10+ Year Member



Yeah Working Now