Need help with JavaScript Replace Method for multiple textboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Barnes

    Need help with JavaScript Replace Method for multiple textboxes

    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>Untitl ed Document</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">

    <script language="javas cript">

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

    </head>

    <body>

    <form name="form1" method="post" action="JStest_ redirect.asp">
    <p>fname:
    <input type="text" name="textfield 1" size="20">
    </p>
    <p>lname:
    <input type="text" name="textfield 2" size="20">
    </p>
    <p>
    <input onclick="return stringReplace(f orm)" type="submit"
    name="Submit" value="Submit">
    </p>

    </form>
    </body>
    </html>
  • Mick White

    #2
    Re: Need help with JavaScript Replace Method for multiple textboxes

    Barnes wrote:[color=blue]
    > 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>Untitl ed Document</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    >
    > <script language="javas cript">
    >
    > function stringReplace(f orm) {
    > var replaceStr = form.textfield1 .value
    > var pattern = /\'/g;
    > form.textfield1 .value = replaceStr.repl ace(pattern, "''");
    > }
    > </script>
    >[/color]
    function stringReplace(f orm,identifier) {
    f=form.length
    while(f--){
    if(form[f].type=="text" && form[f].name.indexOf(i dentifier)!=-1){
    form[f].value = form[f].value.replace(/\'/g;, "''");
    }
    }
    }
    <input onclick="string Replace(this.fo rm,'textfield') " type="submit"
    name="Submit" value="Submit">

    Mick
    [color=blue]
    > </head>
    >
    > <body>
    >
    > <form name="form1" method="post" action="JStest_ redirect.asp">
    > <p>fname:
    > <input type="text" name="textfield 1" size="20">
    > </p>
    > <p>lname:
    > <input type="text" name="textfield 2" size="20">
    > </p>
    > <p>
    > <input onclick="return stringReplace(f orm)" type="submit"
    > name="Submit" value="Submit">
    > </p>
    >
    > </form>
    > </body>
    > </html>[/color]

    Comment

    • Barnes

      #3
      Re: Need help with JavaScript Replace Method for multiple textboxes

      Mick White <mwhite13@BOGUS rochester.rr.co m> wrote in message news:<renFc.303 5$iJ4.2167@twis ter.nyroc.rr.co m>...[color=blue]
      > Barnes wrote:[color=green]
      > > 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>Untitl ed Document</title>
      > > <meta http-equiv="Content-Type" content="text/html;
      > > charset=iso-8859-1">
      > >
      > > <script language="javas cript">
      > >
      > > function stringReplace(f orm) {
      > > var replaceStr = form.textfield1 .value
      > > var pattern = /\'/g;
      > > form.textfield1 .value = replaceStr.repl ace(pattern, "''");
      > > }
      > > </script>
      > >[/color]
      > function stringReplace(f orm,identifier) {
      > f=form.length
      > while(f--){
      > if(form[f].type=="text" && form[f].name.indexOf(i dentifier)!=-1){
      > form[f].value = form[f].value.replace(/\'/g;, "''");
      > }
      > }
      > }
      > <input onclick="string Replace(this.fo rm,'textfield') " type="submit"
      > name="Submit" value="Submit">
      >
      > Mick
      >[color=green]
      > > </head>
      > >
      > > <body>
      > >
      > > <form name="form1" method="post" action="JStest_ redirect.asp">
      > > <p>fname:
      > > <input type="text" name="textfield 1" size="20">
      > > </p>
      > > <p>lname:
      > > <input type="text" name="textfield 2" size="20">
      > > </p>
      > > <p>
      > > <input onclick="return stringReplace(f orm)" type="submit"
      > > name="Submit" value="Submit">
      > > </p>
      > >
      > > </form>
      > > </body>
      > > </html>[/color][/color]


      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(f orm)
      {
      var replaceStr = form.value
      var patter = /\'/g;
      form.value = replaceStr.repl ace(pattern, "'");
      var elem = form.elements[i];

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

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

      </script>

      ....html...<for m method="POST" action="ER_AddP ost.asp" name="form1"
      onsubmit="strin gReplace(this.f orm)">

      Comment

      Working...