Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace multiple unwanted characters in javascript function

Status
Not open for further replies.

lexx2gee

Programmer
Mar 1, 2005
56
GB
Hi everyone,

a bit stumped with this but Iam sure it is probably simple if you work with javascript a lot.

I have written a peice of code to carry out a function on my website which basically submits the variables from an input to an asp form and then displays a message to say thanks for your comments without refreshing the active page.

Only problem is I know javascript doesnt like it if there are & symbols or ' symbols in the input box when it is sending as it takes them to be code rather than text.

So I added the following to my function

<!--**** start of feedback code **** -->

<script type="text/javascript">

function sendRequest(feedback,commentDivId)
{

var pageLocation=window.location.pathname;
var pageLocation = pageLocation.substring(pageLocation.lastIndexOf('/') + 1);
var strFeedback = feedback.value;
strFeedback = strFeedback.replace("&","&amp;");
strFeedback = strFeedback.replace("'","''");



if (feedback.value=='')
{
alert('Please enter your comments then submit.')
return false;
}
else
{
// create XML for HTTP POST
var oDomDoc = Sarissa.getDomDocument();
var xmlString = "<request><feedback>"+strFeedback+"</feedback><pagelocation>"+pageLocation+"</pagelocation></request>";
oDomDoc = (new DOMParser()).parseFromString(xmlString, "text/xml");

// debug-target: popup XML request for debugging

// create XMLHTTPRequest object and POST xml
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", " false);
xmlhttp.send(oDomDoc);

// enable following line
var oDomResultDoc = xmlhttp.responseXML;

// debug-target display xml response result
//alert("XML HTTP Response: "+Sarissa.serialize(oDomResultDoc));

// initialize result
var elResult = oDomResultDoc.getElementsByTagName("return");
var returnValue = elResult[0].getElementsByTagName("feedbackentered")[0].firstChild.nodeValue;

if (returnValue=="true")
{
document.getElementById('customerFeedbackRight').innerHTML='&nbsp;'
commentDivId.innerHTML = '<p class="text" style="width=559px; float:left; margin-top:7px;">Thank you for your comments</p>';
}
else
commentDivId.innerHTML = '<p style="width=559px; float:left; margin-top:7px;">There is problem.</p>';

}
}

</script>

<!--**** end of feedback code **** -->

Now if I type

hello & welcome

it will work fine

but if i type hello & welcome & hello

it will not submit! How can i change the replacement string to cover multiple characters?

Thanks a lot
 
Just totally figured this out, i was missing the regular expression!

Doh!

Thanks anyway, Monday morning what can I say
 
Maybe you should have a look at escape/unescape functions.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top