Need help with JavaScript Replace Method for multiple textboxes

Discussion in 'Javascript' started by Barnes, Jul 3, 2004.

  1. Barnes

    Barnes Guest

    Hi,

    Can anyone please tell me how I can use the replace method to replace
    a character if it occures in more than one textbox without having to
    write separate function for each textbox.

    The code below is the basic way to use the replace method but it only
    allows for one textbox.

    I'm sure I need a loop in there but I'm not sure how to use that
    without affecting the replace method.

    Any help would be greatly appreciated!

    ---------------------------------------------
    <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 3, 2004
    #1
    1. Advertising

  2. Barnes

    Mick White Guest

    Barnes wrote:
    > Hi,
    >
    > Can anyone please tell me how I can use the replace method to replace
    > a character if it occures in more than one textbox without having to
    > write separate function for each textbox.
    >
    > The code below is the basic way to use the replace method but it only
    > allows for one textbox.
    >
    > I'm sure I need a loop in there but I'm not sure how to use that
    > without affecting the replace method.
    >
    > Any help would be greatly appreciated!
    >
    > ---------------------------------------------
    > <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>
    >

    function stringReplace(form,identifier) {
    f=form.length
    while(f--){
    if(form[f].type=="text" && form[f].name.indexOf(identifier)!=-1){
    form[f].value = form[f].value.replace(/\'/g;, "''");
    }
    }
    }
    <input onclick="stringReplace(this.form,'textfield')" type="submit"
    name="Submit" value="Submit">

    Mick

    > </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>
    Mick White, Jul 3, 2004
    #2
    1. Advertising

  3. Barnes

    Barnes Guest

    Mick White <> wrote in message news:<renFc.3035$>...
    > Barnes wrote:
    > > Hi,
    > >
    > > Can anyone please tell me how I can use the replace method to replace
    > > a character if it occures in more than one textbox without having to
    > > write separate function for each textbox.
    > >
    > > The code below is the basic way to use the replace method but it only
    > > allows for one textbox.
    > >
    > > I'm sure I need a loop in there but I'm not sure how to use that
    > > without affecting the replace method.
    > >
    > > Any help would be greatly appreciated!
    > >
    > > ---------------------------------------------
    > > <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>
    > >

    > function stringReplace(form,identifier) {
    > f=form.length
    > while(f--){
    > if(form[f].type=="text" && form[f].name.indexOf(identifier)!=-1){
    > form[f].value = form[f].value.replace(/\'/g;, "''");
    > }
    > }
    > }
    > <input onclick="stringReplace(this.form,'textfield')" type="submit"
    > name="Submit" value="Submit">
    >
    > Mick
    >
    > > </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>



    Mick,
    Thanks for your quick reply.

    I used your suggestion and developed a for loop but the apostrophes
    are not getting replace when the form is submitted. Can you please
    take a look at the for loop code and see if you can find anything
    wrong. Thank you!!

    -------------

    <script Language = "JavaScript" Type="text/javascript">

    function stringReplace(form)
    {
    var replaceStr = form.value
    var patter = /\'/g;
    form.value = replaceStr.replace(pattern, "'");
    var elem = form.elements;

    for (i = 0; i < form.length; i++)
    }

    if(form.type == "text")
    {
    stringReplace(elem);
    }
    }
    }

    </script>

    ....html...<form method="POST" action="ER_AddPost.asp" name="form1"
    onsubmit="stringReplace(this.form)">
    Barnes, Jul 8, 2004
    #3
    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. Teep
    Replies:
    1
    Views:
    308
    Trevor Benedict R
    Jun 17, 2004
  2. TN Bella
    Replies:
    1
    Views:
    2,469
    Edward
    Jun 18, 2004
  3. Brian Blais
    Replies:
    1
    Views:
    373
    Bruno Desthuilliers
    Jun 27, 2006
  4. Greg Ewing
    Replies:
    2
    Views:
    342
    Dieter Maurer
    Jun 29, 2006
  5. Drew
    Replies:
    7
    Views:
    198
    Ray Costanzo [MVP]
    Nov 10, 2004
Loading...

Share This Page