SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Remove & and #

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove & and #

    Hi,

    I have a text input box which users use to submit a string. I would like to use javascript (jquery) so that if the string contains any & or # these get automatically removed. Does anyone know how to do this?

    Thanks

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Something like this would work. You'll have to use regex for the character removal. Typically, you state what characters you want, the just remove everything else. In this example, you keep characters a-z, A-Z and numbers 0-9:

    Code JavaScript:
    $("#thetextboxid").keypress(function(){
         $("#thetextboxid").value($("#thetextboxid").value().
    replace(/[^a-zA-Z 0-9]+/g,''));
    });

    If you really want to just remove just the # and &, this should do it:

    Code JavaScript:
    $("#thetextboxid").keypress(function(){
         $("#thetextboxid").value($("#thetextboxid").value().
    replace(/[\&\#]+/g,''));
    });

    What this code is doing is that when a key is press in the textbox with the id of "thetextboxid", it replaces the characters specified in the regular expression with a blank, effectively removing the character from the string.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this but nothing seemed to happen:

    Code:
    <html>
    
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    
    
    <script type="text/javascript">
    $("#remove").keypress(function(){
         $("#remove").value($("#remove").value().
    replace(/[\&\#]+/g,''));
    });
    </script>
    
    </head>
    
    <body>
    
    
    <form>
    <input id="remove" type="text"></input>
    <input type="submit" value="click"></input>
    </form>
    </body>
    
    </html>

  4. #4
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    The replace function should be on the same line as the jquery call

    Code:
    $("#remove").value($("#remove").value().replace(/[\&\#]+/g,''));
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


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
  •