JavaScript Replace() Method

B

Barnes

Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form?

Here is the scenario:

I have a form that cannot accept apostrophes. I want to use the replace() so that the apostrophe is
automatically replace with two '' . Reason being--SQL Server does not like apostrophes being sent to database.

I've tried to do this on the server side in the SQL area of the ASP page by writing a function (with some great help)
but I can seem to get it to work. That's is why I want do try on the client side.

I've already made an attempt and the replace() method does work. The problem is the form has an Action = "bla.asp"
This is the redirect page. But the page doesn't redirect because in the Input tag, I have an onclick = "return
stringReplace(this.form)"

So here is my delimma--I want to be able to replace the character before it redirects to the other .asp page.

Any ideas? Below is the code:
---------------------------------------------------
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">

function stringReplace(form) {
var replaceStr = form.textfield1.value
var pattern = /\'/g;
form.textfield1.value = replaceStr.replace(pattern, "''");
}
</script>

</head>

<body>

<form name="form1" method="post" action="JStest_redirect.asp">
<p>fname:
<input type="text" name="textfield1" size="20">
</p>
<p>lname:
<input type="text" name="textfield2" size="20">
</p>
<p>
<input onclick="return stringReplace(form)" type="submit" name="Submit" value="Submit">
</p>

</form>
</body>
</html>
 
A

Alan Howard

You could call your stringReplace method as the onBlur handler for each
control needing it - but your attempt to do this server-side is the correct
way approach - show us your server-side code that wasn't working.

Alan

Barnes said:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form?

Here is the scenario:

I have a form that cannot accept apostrophes. I want to use the replace() so that the apostrophe is
automatically replace with two '' . Reason being--SQL Server does not like
apostrophes being sent to database.
I've tried to do this on the server side in the SQL area of the ASP page
by writing a function (with some great help)
but I can seem to get it to work. That's is why I want do try on the client side.

I've already made an attempt and the replace() method does work. The
problem is the form has an Action = "bla.asp"
This is the redirect page. But the page doesn't redirect because in the
Input tag, I have an onclick = "return
stringReplace(this.form)"

So here is my delimma--I want to be able to replace the character before
it redirects to the other .asp page.
 
E

Evertjan.

=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
microsoft.public.inetserver.asp.general:
I've already made an attempt and the replace() method does work. The
problem is the form has an Action = "bla.asp" This is the redirect
page. But the page doesn't redirect because in the Input tag, I have
an onclick = "return stringReplace(this.form)"

Just skip the word "return" like this:

onclick = "stringReplace(this.form)"
So here is my delimma--I want to be able to replace the character
before it redirects to the other .asp page.

You should not ask a clientside question on this NG, really.

It is like asking the bus company how to repair your own car,
because you planned to go by bus first or next time.
 
B

Barnes

How would I check for all textfields, not just the first one.

The blank in the code below would be where the textfield name would go but can I generalize this name? I've tried text, type, input...but I'm not sure what JavaScript name would go here. Also, If I use onblur, can I use the same function or do I have to make a function for every textfeild--this would be too much.

function stringReplace(form) {
var replaceStr = form.______.value
var pattern = /\'/g;
form._______.value = replaceStr.replace(pattern, "''");
}
</script>

Thank you in advance for your help!
 
E

Evertjan.

=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
microsoft.public.inetserver.asp.general:
How would I check for all textfields, not just the first one.

The blank in the code below would be where the textfield name would go
but can I generalize this name? I've tried text, type, input...but I'm
not sure what JavaScript name would go here. Also, If I use onblur,
can I use the same function or do I have to make a function for every
textfeild--this would be too much.

function stringReplace(form) {
var replaceStr = form.______.value
var pattern = /\'/g;
form._______.value = replaceStr.replace(pattern, "''");
}
</script>

Thank you in advance for your help!

This is all clientside code.
Why would you want to use a serverside NG?

Please follow up elsewhere.
 
B

Barnes

Here is the server side script that you were asking me about. This is generation an "Object undefined" error and I can't seem to correct it. If you can offer help on this, then I won't have to do the replace method in JavaScript on the client side.
-------------------------------------------------
function replace(string)
{
var pattern = /\'/g;
var newString = string.replace(pattern, "''");
return newString;
}

// Create a database connection.
Database = Server.CreateObject ("ADODB.Connection");
//Database.Open ("dsn=INTRANET");
Database.Open ("driver=SQL .....

// Create a recordset of all information in this table.
DirectoryRecordSet = Server.CreateObject ("ADODB.RecordSet");

// Construct the SQL Query
strSQL = "INSERT INTO " +
"ProductEvalRequest " +
"(RequestorName, RequestorEmail, OutsideSalesRepName, OutsideSalesRepEmail, InsideSalesRepName, InsideSalesRepEmail, " +
"ReportingMgrName, ReportingMgrEmail, CustomerName, CustomerEmail, DescProduct, ProductDollarAmt, " +
"ValueEval, DealBook, DealStage, ApprxStorageOpp, AcctRev , DateEval, DurationEval, Approval) " +
"VALUES " +
"('" + replace(RN) + "', " +
"'" + replace(RE) + "', " +
"'" + replace(Request.Form("txtOutsideSalesRepName")) + "', " +
"'" + replace(Request.Form("txtOutsideSalesRepEmail") + "', " +
"'" + replace(Request.Form("txtInsideSalesRepName") + "', " +
"'" + replace(Request.Form("txtInsideSalesRepEmail") + "', " +
"'" + replace(Request.Form("txtReportingMgrName") + "', " +
"'" + replace(Request.Form("txtReportingMgrEmail") + "', " +
"'" + replace(Request.Form("txtCustomerName") + "', " +
"'" + replace(Request.Form("txtCustomerEmail") + "', " +
"'" + replace(Request.Form("txtDescProduct") + "', " +
"'" + replace(Request.Form("txtProductDollarAmt") + "', " +
"'" + replace(Request.Form("txtValueOfDeal") + "', " +
"'" + replace(Request.Form("txtDealExpBook") + "', " +
"'" + replace(Request.Form("txtStageDeal") + "', " +
"'" + replace(Request.Form("txtApproxStorageOpp") + "', " +
"'" + replace(Request.Form("txtAcctRev") + "', " +
"'" + replace(Request.Form("txtDateEval") + "', " +
"'" + replace(Request.Form("txtDurationEval") + "', " +
"'" + replace(Request.Form("txtApproval") + "')";




//Response.Write ("<p>" + strSQL + "\n");

//Response.Write(replace(string));

DirectoryRecordSet.Open (strSQL, Database);


%>
 
E

Evertjan.

=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
microsoft.public.inetserver.asp.general:
Do you know of a JavaScript NG at MSDN? I tried to look for one but
can't find it.

[please do not toppost on usenet]

Dunno about MDSDN, will not touch it.

You are here on usenet, venerable old usenet with many NGs.

Try:

comp.lang.javascript
 
B

Barnes

Ok, that's Google, right? Thanks.

Evertjan. said:
=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
microsoft.public.inetserver.asp.general:
Do you know of a JavaScript NG at MSDN? I tried to look for one but
can't find it.

[please do not toppost on usenet]

Dunno about MDSDN, will not touch it.

You are here on usenet, venerable old usenet with many NGs.

Try:

comp.lang.javascript
 
B

Bob Barrows [MVP]

microsoft.public.scripting.jscript
Do you know of a JavaScript NG at MSDN? I tried to look for one but
can't find it.

Your help is greatly appreciated.
 
E

Evertjan.

=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
microsoft.public.inetserver.asp.general:
Evertjan. said:
=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
[please do not toppost on usenet]

Dunno about MDSDN, will not touch it.

You are here on usenet, venerable old usenet with many NGs.

Try:

comp.lang.javascript
Ok, that's Google, right? Thanks.

No, it is Usenet. Usenet presedes the Web by many years.

You could access that via the web and Google, but you have much more
flexiblility using a dedicated News server like Xnews or Agent.

And please do not respond with an answer on top of my mail.
That topposting is "not done" on usenet.
 
B

Barnes

And please do not respond with an answer on top of my mail.
That topposting is "not done" on usenet.
SORRY! I didn't know that was called topposting and not allowed here. I'm new to these newsgroups. I'll be more careful. Thanks for your patience!
 
A

Alan Howard

Which line throws the error?



Barnes said:
Here is the server side script that you were asking me about. This is
generation an "Object undefined" error and I can't seem to correct it. If
you can offer help on this, then I won't have to do the replace method in
JavaScript on the client side.
-------------------------------------------------
function replace(string)
{
var pattern = /\'/g;
var newString = string.replace(pattern, "''");
return newString;
}

// Create a database connection.
Database = Server.CreateObject ("ADODB.Connection");
//Database.Open ("dsn=INTRANET");
Database.Open ("driver=SQL .....

// Create a recordset of all information in this table.
DirectoryRecordSet = Server.CreateObject ("ADODB.RecordSet");

// Construct the SQL Query
strSQL = "INSERT INTO " +
"ProductEvalRequest " +
"(RequestorName, RequestorEmail, OutsideSalesRepName,
OutsideSalesRepEmail, InsideSalesRepName, InsideSalesRepEmail, " +
"ReportingMgrName, ReportingMgrEmail, CustomerName, CustomerEmail,
DescProduct, ProductDollarAmt, " +
 
G

Gervin

This is how I usually replace single quotes with double single quotes before
insert into sql server on the server side.

sqlstr = "insert into mytable(myfieldname) values ('" & replace(mydata, "'",
"''") & "')"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top