...

View Full Version : Replacing Apostropes in a textfield.



florida
08-26-2004, 01:27 PM
I have a form textfield where I want to substitute all apostrophes with a blank.

So if someone enters State's in my textfield called Comments it will replace the entry with State s.

Here is my attempt for implementing this on the Comments textfield only:



<script>
function CommentsReplace()
{
document.myformName.Comments.replace(/'/,' ');
}
</script>


....
<form name="myFormName" action="myactionName.cfm" onsubmit="return CommentsReplace();">
...


Please advise why this is not working and how I can get it to work??

liorean
08-26-2004, 01:33 PM
First of all, you need to change the .value property of the field, not the field itself.

Second, String.replace is nondestructive, meaning it leaves the orignial as is but returns the string wit5h the replacements done. So, you need to do something like
Field.value=Field.value.replace(re);

jamescover
08-26-2004, 01:43 PM
<script type="text/javascript">
<!--

function reCom(){
var doc = document['myFormName']['Comments'];
var replace = doc.value.replace(/'/,' ');
doc.value = replace;
alert(replace);
}

//-->
</script>


....
<form name="myFormName">
<input type="text" name="Comments" />
<input type="button" onclick="javascript:reCom();" />
</form>
...



You also need to watch your casing:


document.myformName.Comments.replace(/'/,' ');
}
</script>
....
<form name="myFormName" action="myactionName



-james

florida
08-26-2004, 02:18 PM
Thanks!

glenngv
08-26-2004, 02:21 PM
Or simply pass the form reference to the function.


function CommentsReplace(oForm)
{
oForm.Comments.value=oForm.Comments.value.replace(/'/,' ');
}
...
<form onsubmit="return CommentsReplace(this);">

florida
08-26-2004, 05:26 PM
thanks for the additional information.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum