Results 1 to 2 of 2

Thread: Detect Characters in String

  1. #1
    Join Date
    May 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Detect Characters in String

    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:


    Code:
    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    You are doing this sort of right:

    Code:
      var str = document.forms.Form1.header.value;
      str.replace(/;/, '');
    It should probably be:

    Code:
      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:

    Code:
      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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •