JavaScript Replace() Method

Discussion in 'ASP General' started by Barnes, Jul 2, 2004.

  1. Barnes

    Barnes Guest

    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>
     
    Barnes, Jul 2, 2004
    #1
    1. Advertising

  2. Barnes

    Alan Howard Guest

    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" <> wrote in message
    news:...
    > 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>
    >
     
    Alan Howard, Jul 2, 2004
    #2
    1. Advertising

  3. Barnes

    Evertjan. Guest

    =?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)
     
    Evertjan., Jul 2, 2004
    #3
  4. Barnes

    Barnes Guest

    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" <> wrote in message
    > news:...
    > > 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>
    > >

    >
    >
    >
     
    Barnes, Jul 2, 2004
    #4
  5. Barnes

    Evertjan. Guest

    =?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)
     
    Evertjan., Jul 2, 2004
    #5
  6. Barnes

    Barnes Guest

    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" <> wrote in message
    > news:...
    > > 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>
    > >

    >
    >
    >
     
    Barnes, Jul 2, 2004
    #6
  7. Barnes

    Evertjan. Guest

    =?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)
     
    Evertjan., Jul 2, 2004
    #7
  8. Barnes

    Barnes Guest

    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)
    >
     
    Barnes, Jul 2, 2004
    #8
  9. 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"
     
    Bob Barrows [MVP], Jul 2, 2004
    #9
  10. Barnes

    Evertjan. Guest

    =?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)
     
    Evertjan., Jul 3, 2004
    #10
  11. Barnes

    Barnes Guest


    > 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!
     
    Barnes, Jul 3, 2004
    #11
  12. Barnes

    Alan Howard Guest

    Which line throws the error?



    "Barnes" <> wrote in message
    news:...
    > 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" <> wrote in message
    > > news:...
    > > > 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>
    > > >

    > >
    > >
    > >
     
    Alan Howard, Jul 3, 2004
    #12
  13. Barnes

    Gervin Guest

    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, "'",
    "''") & "')"
     
    Gervin, Jul 9, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    394
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    357
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,570
    Masudur
    Feb 18, 2008
  4. Barnes
    Replies:
    2
    Views:
    143
    Barnes
    Jul 8, 2004
  5. Mark Szlazak

    On Javascript Replace Method.

    Mark Szlazak, Jul 29, 2007, in forum: Javascript
    Replies:
    8
    Views:
    109
    Mark Szlazak
    Jul 31, 2007
Loading...

Share This Page