By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

JavaScript Replace() Method

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
=?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

P: n/a
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

P: n/a
=?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

P: n/a
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

P: n/a
=?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

P: n/a
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

P: n/a
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

P: n/a
=?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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.