Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2007, 12:17 PM   PM User | #1
coder81
New to the CF scene

 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
coder81 is an unknown quantity at this point
About "value.replace" for Textareas

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:

Code:
onkeyup="this.value=this.value.replace('"','')"
and also...

Code:
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:

Code:
<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?
coder81 is offline   Reply With Quote
Old 01-09-2007, 01:10 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
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
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 01-09-2007, 10:52 PM   PM User | #3
coder81
New to the CF scene

 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
coder81 is an unknown quantity at this point
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>

Last edited by coder81; 01-09-2007 at 11:25 PM.. Reason: updated
coder81 is offline   Reply With Quote
Old 01-10-2007, 12:09 AM   PM User | #4
rubenbuhr
Regular Coder

 
Join Date: Oct 2006
Posts: 107
Thanks: 3
Thanked 5 Times in 5 Posts
rubenbuhr is an unknown quantity at this point
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>
rubenbuhr is offline   Reply With Quote
Old 01-10-2007, 03:47 AM   PM User | #5
coder81
New to the CF scene

 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
coder81 is an unknown quantity at this point
That Works Great !!! - Thank You So Much !!!
coder81 is offline   Reply With Quote
Old 01-10-2007, 04:38 PM   PM User | #6
rubenbuhr
Regular Coder

 
Join Date: Oct 2006
Posts: 107
Thanks: 3
Thanked 5 Times in 5 Posts
rubenbuhr is an unknown quantity at this point
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>
rubenbuhr is offline   Reply With Quote
Old 01-10-2007, 04:44 PM   PM User | #7
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Wow, demanding

ask nicely!

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

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:02 AM.


Advertisement
Log in to turn off these ads.