PDA

View Full Version : Detect Characters in String



T Horton
05-17-2008, 08:50 PM
Hello All

I am currently developing an online email scanning system, but have come across a problem which I was wondering whether someone could help me with.

Basically the site asks a user to copy and paste email headers in to a textarea. This works fine as it is 90% of the time. However, on some ocassions I get error messages (500 Internal server error). The reason for this appears to be the fact that if the header you have pasted in contains ";" halfway across a line, it raises the error.

Unfortunately I cannot get the page the info is submitted to, to detect and repair the problem before the error is raised, as I get the internal error 500 etc. The solution as it stands would appear to be that I have to put in a javascript check on the entry page to ensure it inserts a new line after every ";" in the textarea.

I would really appreciate it if someone could tell me how this is done. I have tried all sorts (Replace etc.) and it still doesn't want to work.

This is the code as it stands at the moment:




function checkform(){
if(document.forms.Form1.header && document.forms.Form1.header.value =='')
{
alert('You have not entered an email header. Please try again.');
return false;
}
else {
var str = document.forms.Form1.header.value;
str.replace(/;/, '');
return true;
}
}


If you want to check the site as it stands, the address at the moment is www.detect.thehortonfamily.co.uk

Anyone's help would be greatly appreciated.

Best Regards


Tom

jscheuer1
05-18-2008, 01:23 AM
You are doing this sort of right:


var str = document.forms.Form1.header.value;
str.replace(/;/, '');

It should probably be:


str.replace(/;/gm, '');

The gm simply means to replace this globally on every line. Very old browsers will barf on the m (multi-line) part though. So, if it is only all one long line, just use g.

However, even with that enhancement, simply declaring:


str.replace(/;/gm, '');

doesn't really do anything. It does strip ; from str, but it doesn't change str, and it doesn't set anything to the stripped value of str.

So you might want to do:


function checkform(){
if(document.forms.Form1.header && document.forms.Form1.header.value =='')
{
alert('You have not entered an email header. Please try again.');
return false;
}
else {
var str = document.forms.Form1.header.value;
document.forms.Form1.header.value = str.replace(/;/gm, '');
return true;
}
}

This will be effective, as long as it executes before ; can cause an error.