Click to See Complete Forum and Search --> : validate and replace double quotes??


sanjuT
01-15-2003, 02:25 PM
i want to be able to have a JS function that i can call that would check if there was a quote or double quote in a textarea on my form.

if there is one, i would like to replace it with either \" or \'.

this would prevent my servlet from cutting of the endof the info (i think).

if anyone can help out, i'd greatly appreaciate it!!
Thanks!

khalidali63
01-15-2003, 02:40 PM
the method below should do it for you..
Khalid


<form name="form1">
<textarea name="tarea"></textarea>
<input type="Button" value="process" onclick="process();"></input>
</form>
<script language="JavaScript1.2">
function process(){
var data = document.form1.tarea.value;
alert("b4 processing \n"+data);
data = data.replace(/\"/g,"\\\"");
data = data.replace(/\'/g,"\\\'");
alert("data after replacements \n"+data);
}
</script>

sanjuT
01-15-2003, 03:25 PM
Thanks!!! It worked for the double quotes perfectly!!! but it didn't work for the single quotes for some reason.

i have one more thing to add:

if i want to validate 2 textareas, is there an easy way to alter the code?

THANKS A LOT!!!!

oh, it looks like u intended a message to alert the user to the change? if so, that part didn't work..
no biggie!

khalidali63
01-15-2003, 04:14 PM
code works perfectly in ns6+ IE,you must have lost something in copy and pasting
:D

2 textareas


<body>
<form name="form1">
<textarea name="tarea1"></textarea>
<textarea name="tarea2"></textarea>
<input type="Button" value="process" onclick="process();"></input>
</form>
<script language="JavaScript1.2">
function process(){
var data1 = parseData(document.form1.tarea1.value);//text area 1
var data2 = parseData(document.form1.tarea2.value);//text area 2
alert("data after replacements \n\tData1\n"+data1+"\n\tData1\n"+data2);
}

function parseData(data){
data = data.replace(/\"/g,"\\\"");
data = data.replace(/\'/g,"\\\'");
return data;
}
</script>

sanjuT
01-16-2003, 10:13 AM
:( not sure where i'm messing it up.

if i'm not getting a message box when i submit, does that mean the function is not triggering?

here's my form tag:

<form name="rfss" method="post" action="/servlet/RFSSConfirm"
onClick="return process();"
onSubmit=" this.SystemID.optional = true;

return validate(this);">

;) i have renamed the form in the scripts..

not sure if u are familiar with this, but here's where the data from the 2 textareas are used in my servlet:

msgText1= msgText1 + "<input type = hidden name = Description value = " + req.getParameter("Description") + ">";
msgText1= msgText1 + "<input type = hidden name = Benefits value = " + req.getParameter("Benefits") + ">";


;) here's the text i have been using to test in each textarea:

testing "the" form 'for' quotes'

;) and finally, here's the code u supplied that i used (i changed a few things to work in my form):

<script language="JavaScript1.2">
function process(){
var data1 = parseData(document.rfss.Description.value);//text area 1
var data2 = parseData(document.rfss.Benefits.value);//text area 2
//alert("data after replacements \n\tData1\n"+data1+"\n\tData1\n"+data2);
}

function parseData(data){
data = data.replace(/\"/g,"\\\"");
data = data.replace(/'/g,"\\'");
return data;
}
</script>



:confused: does this script rename the form data? (ie instead of Descriptions and Benefits, r they renamed to data1 and data2)?

THANKS A LOT AGAIN!!! you have been helping me out quite a bit, and i really appreaciate it!:)

Webskater
01-16-2003, 03:36 PM
Your question:
if i want to validate 2 textareas, is there an easy way to alter the code?
When you want to use the same function for more than one element ...
You can tell which element called the function as follows:
var Sender = event.srcElement;
You then use the variable in your code so you can use the function for as many elements as you like.
I have taken the liberty of using khalidali63's code already posted.

e.g.

function process(){
var Sender = event.srcElement;
var data = Sender.value;
alert("b4 processing \n"+data);
data = data.replace(/\"/g,"\\\"");
data = data.replace(/'/g,"\\'");
alert("data after replacements \n"+data);
Sender.value = data;
}

sanjuT
01-21-2003, 10:23 AM
ok, I'M REALLY STUMPED.. (oops CAPS lock)

i'm not sure what i'm doing wrong.

Is there a way for the servlet to fix the quote problem, or is it a better idea to do it client-side?

there are a total of 3 servlets.

1. the Confirm servlet - The whole string is fine, no cut-offs. takes info from HTML form and displays it.

2. Auth servlet - takes info from Confirm page and creates a webpage with the form data in it. This is where i get the problem.

3. the Send servlet - final submission of the data. Again, since it is taking the data from the Auth page, it is cut-off.


Does this make any sense??

I am going bonkers!!!

Thanks all!

khalidali63
01-21-2003, 01:53 PM
can you post a link of actually not working page,so that I or some one else might be able to take a look.
And for the second part of your question,
yes you should be able to use escape characters in Java as well as in JavaScript.

Or may be if you can not put link here,email me the exct page that is not working and a test string as well.


Khalid