|
| |
|
|
kt
Posts: 199 Joined: 11/3/2004 Status: offline
|
Javascript Replace problem - 11/2/2009 21:25:34
Hi everyone I have a javascript which I use to pass information to a database. I need to replace all the ampersands and apostrophes in the data, otherwise the script breaks. But I'm having a little trouble getting the replace function to work properly. Here's the whole script - I realise it's not very elegant and could no doubt be improved in other ways, but I'm new to Javascript, and my concern now is the replace function. $(function() {
$('.error').hide();
$('.sub_spinner').hide();
$('input.text-input').css({backgroundColor:"#FFFFFF"});
$('input.text-input').focus(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$(".button").click(function() {
$('.error').hide();
var companypre = $("input#company").val(); =
var company = companypre.replace(/"&"/g, "%26");
if (company== "") {
$("label#company_error").show();
$("input#company").focus();
return false;
}
var citypre = $("input#city").val();
var city = citypre.replace(/"&"/g, "%26");
var country = $("select#country").val();
var detailspre = $("textarea#details").val();
var detailspre2 = detailspre.replace(/(\r\n|\r|\n)/g, '<br />')
var details = detailspre2.replace(/"&"/g, "%26");
if (details== "") {
$("label#details_error").show();
$("textarea#details").focus();
return false;
}
$('#test_null_submit').hide();
$('.sub_spinner').show();
var dataString = 'company=' + company.replace(/( )/g, '%20') + '&city=' + city.replace(/( )/g, '%20') + '&country=' + country.replace(/( )/g, '%20') + '&details=' + details.replace(/( )/g, '%20');
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "/includes/process.asp",
data: dataString,
success: function() {
$('.sub_spinner').hide();
$('#contact_form').html("<p class='conf_message' style='height:200px;margin-left:5px;color:#363636'></p>");
$('.conf_message').html("<br />Thank you.")
.hide()
.fadeIn(350, function() {
$('.conf_message');
});
}
});
return false;
});
});
This works correctly to replace carriage returns with <br> in the textarea, and to replace spaces with %20 in every field (done in the datastring). What doesn't work is replacing the ampersands. As it is, it replaces all of the ampersands from the company field, but only the first one in the city field, and then the country field doesn't get saved at all. if there are no ampersands in the fields, of course it works fine. I've tried also /(&)/g and /("&")/g but it makes no difference. Can anyone tell me why the global function only works for the first variable? Many thanks for your help. kt
|
|
|
|
kt
Posts: 199 Joined: 11/3/2004 Status: offline
|
RE: Javascript Replace problem - 11/4/2009 15:38:25
Thanks!
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|