SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: text validation

  1. #1
    SitePoint Enthusiast milko_leporis's Avatar
    Join Date
    Apr 2005
    Location
    Kisac, Vojvodina
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    text validation

    How to change " , ' and & into &quote; , ´ and &amp?
    I tryed this

    var string = '"javascript&XML";'
    var text= '"';
    string.replace(text, '&quote;');

    but it isn't working, I get string without ' , " or &, I tryed with escaping chars with / but that isn't working either.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First argument in String.replace needs to be a regular expression

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Location
    France
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    And what about something like this :
    string=string.replace("&","&quote;");

    Don't forget to pass the replacement of "&" first, or you'll have some surprises...

  4. #4
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      function toEntities(str)
      {
          toFind = new Array(    '&',
                              '"',
                              "'"
                          );
          toReplace = new Array(    '&amp',
                                  '&quote',
                                  '&acute'
                          );
      
          for(i=0, max=toFind.length; i < max; i++) {
              var re = new RegExp(toFind[i], 'g');
              str = str.replace(re, toReplace[i]);
          }
          return str;
      }

  5. #5
    SitePoint Enthusiast milko_leporis's Avatar
    Join Date
    Apr 2005
    Location
    Kisac, Vojvodina
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you really want to change the apostrophe character into an acute accent? If you want to escape it, you should use &#38;#39; (or &#38;apos; if you're using real XHTML). &#38;acute; is not an apostrophe.
    Birnam wood is come to Dunsinane


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
  •