473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

JavaScript Replace() Method

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>

Jul 19 '05 #1
12 8115
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" <Ba****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
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>

Jul 19 '05 #2
=?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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #3
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!

"Alan Howard" wrote:
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" <Ba****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
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>


Jul 19 '05 #4
=?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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #5
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);
%>

-------------
Thank you in advance for your help!!
"Alan Howard" wrote:
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" <Ba****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
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>


Jul 19 '05 #6
=?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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #7
Ok, that's Google, right? Thanks.

"Evertjan." wrote:
=?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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jul 19 '05 #8
microsoft.public.scripting.jscript

Barnes wrote:
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.

"Evertjan." wrote:
=?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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #9
=?Utf-8?B?QmFybmVz?= wrote on 02 jul 2004 in
microsoft.public.inetserver.asp.general:
"Evertjan." wrote:
=?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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #10

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!
Jul 19 '05 #11
Which line throws the error?

"Barnes" <Ba****@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
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);
%>

-------------
Thank you in advance for your help!!
"Alan Howard" wrote:
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" <Ba****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
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>


Jul 19 '05 #12
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, "'",
"''") & "')"
Jul 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.