Need help with JavaScript Replace Method for multiple textboxes

B

Barnes

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>
 
M

Mick White

Barnes said:
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
 
B

Barnes

Mick White said:
Barnes said:
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)">
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top