Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with JavaScript Replace Method for multiple textboxes

Barnes
Guest
 
Posts: n/a
#1: Jul 23 '05
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>

Mick White
Guest
 
Posts: n/a
#2: Jul 23 '05

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>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>
>[/color]
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
[color=blue]
> </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>[/color]
Barnes
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Need help with JavaScript Replace Method for multiple textboxes


Mick White <mwhite13@BOGUSrochester.rr.com> wrote in message news:<renFc.3035$iJ4.2167@twister.nyroc.rr.com>...[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>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>
> >[/color]
> 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
>[color=green]
> > </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>[/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(form)
{
var replaceStr = form.value
var patter = /\'/g;
form.value = replaceStr.replace(pattern, "'");
var elem = form.elements[i];

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)">
Closed Thread