Forum Moderators: open
<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]
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.
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]