...

View Full Version : About "value.replace" for Textareas



coder81
01-09-2007, 01:17 PM
I have a form named "contacter" and a textarea named "Comments". I need to find a way to prevent quoatations from being placed in the textarea and if the area placed in the textarea; I want them to be automatically deleted.

I have tried to use:


onkeyup="this.value=this.value.replace('"','')"

and also...


onkeyup="this.value=this.value.replace(''','')"

But it will not work and I think the one single or double quotations marks cancels out the other one; when it is typed like this.

I have been looking around and seen a few pieces of script, but have not been able to get them to work.

I tried to create this; but it also did not work:


<script>
function check()
{
if (window.event.keyCode=222) {
document.contacter.Comments.value.length=-1;
return false;
}
else (void())
}
</script>

Any one know how I can get this textarea to automatically delete " and ' character, either onkeyup or onsubmit?

A1ien51
01-09-2007, 02:10 PM
If you are doing this to protect yourself on the server, you better do all of the replacing/escaping there since you can not rely on JavaScript or people like me using your forms.

You probably want to use a regular expression

.replace(/\'/gi,"");

Eric

coder81
01-09-2007, 11:52 PM
Not sure quit how to do that. Can you give me an example on how to use Javascript .replace(/\'/gi,""); with my form fields.

Form Name = contacter
Form Field = Comments

My main goal is to prevent "" and '. The rest of the chars, I am not really worried about.

If you could give me an example of this, that would be very good. - Thanks

I have got this so far; but it erases any value that is typed in the textarea and changes it to something else What am I doing wrong?

<script>
function check()
{
if (document.cf.Comments.value="'") {
document.cf.Comments.value.replace(/\'/gi,"");
return false;
}
else(void(0))
}
</script>
</head>
<body>

<form name="cf">
<textarea name="Comments" onkeyup="check()"></textarea>
</form>

rubenbuhr
01-10-2007, 01:09 AM
try this...

<SCRIPT type="text/javascript">
function check(){
var str=document.cf.Comments.value;
str=document.cf.Comments.value=str.replace(/'/g, " ")
document.cf.Comments.value=str.replace(/"/g, " ")
}
</SCRIPT>

<form name="cf">
<textarea name="Comments" onchange="check()"></textarea>
</form>

coder81
01-10-2007, 04:47 AM
That Works Great !!! - Thank You So Much !!!

rubenbuhr
01-10-2007, 05:38 PM
Good, now help me to make this work for a variable, what how to do syntax???

<SCRIPT type="text/javascript">
function check(){
var K=document.cf.myVar.value;
var str=document.cf.Comments.value;
str=document.cf.Comments.value=str.replace(/K/g, " ")
document.cf.Comments.value=str.replace(/"/g, " ")
}
</SCRIPT>

<form name="cf">
<input id="myVar" value="M" onchange="check()">
<textarea name="Comments" onchange="check()"></textarea>
</form>

A1ien51
01-10-2007, 05:44 PM
Wow, demanding

ask nicely!

Teach yourself about new RegExp() and see if you can figure it out.

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum