www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: how to disable special characters in a textbox/textarea?

Hybrid View

  1. #1
    Join Date
    May 2008
    Posts
    57

    how to disable special characters in a textbox/textarea?

    can anybody please show me how can i disable special characters in a textbox/textarea? im still not very skillful in javascript so i wanted to ask anyone to help me,tnx in advance

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Which special characters? Or maybe it is easier to specify which characters should be allowed. RegExp could be useful for this job

  3. #3
    Join Date
    May 2008
    Posts
    57
    i only want to allow 0-9 and a-z and and ,can you show me an example of what you suggested?tnx

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/JavaScript">
    function valid(f) {
    !(/^[A-z&#209;&#241;0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-z&#209;&#241;0-9]/ig,''):null;
    } 
    </script>
    </head>
    <body><br>
    <form id="myform" action="">
    <input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
    </form>
    </body>
    </html>

  5. #5
    Join Date
    May 2008
    Posts
    57
    wow it worked! tnx your a life saver man,one thing though when i try the and it still get deleted...

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    In works for me. Well yes, I have no Spanish keyboard (thus I can not test onkeyup), but if I copy/paste the characters &#209;&#241;, they remain in the field. But it theory should work the same onkeyup as onblur.

    What charset do you use? iso-8859-1 ?

  7. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Maybe use the unicode versions?
    Code:
    function valid(f) {
    f.value = f.value.replace(/[^a-z\u00D1\u00F10-9]*/ig,'');
    }
    I have no Spanish keyboard either, so I cannot check.

  8. #8
    Join Date
    Sep 2011
    Posts
    2
    How to disallow characters in the textfield?

  9. #9
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by jagh View Post
    How to disallow characters in the textfield?
    As it is described above.

  10. #10
    Join Date
    Sep 2011
    Posts
    2
    Thanks, it works. But the space is not allowed to input. I need space to input two names in textbox/textarea (e.g. Maria Ana). How can i define it to this code:

    !(/^[A-z0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-z0-9]/ig,''):null;

  11. #11
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    !(/^[A-z&#209;&#241;0-9-\s]*$/i).test(f.value)?f.value = f.value.replace(/[^A-z&#209;&#241;0-9-\s]/ig,''):null;
    Read more about regular expressions in javaScript:
    http://lawrence.ecorp.net/inet/samples/regexp-intro.php

  12. #12
    Join Date
    Feb 2013
    Posts
    1
    Hello!

    function valid(f) {
    f.value = f.value.replace(/[^A-z&#209;&#241;0-9-\s]/ig,'')
    }
    Works perfect! But, how can i change this to can write . : ! , characters?
    Can anyone help me?

  13. #13
    Join Date
    Mar 2013
    Posts
    1
    [A-z&#209;&#46;&#241;0-9-\s\.\:\!]
    This worked for me.

  14. #14
    Join Date
    Jul 2013
    Posts
    1
    Ok, I've tweaked the code a bit and got it close to where I want it, but some characters are still popping up. How do I limit it to just A-z - , . ' and spacebar

    I don't want numbers or any weird special characters, this is for customers to order uniform nametapes on my website, so just what would be in someone's name including Spanish lettering and hyphenated names. Also if they wanted last, first or Last, F. And anything like O'whatever should also be allowed. When I type it in as:

    !(/^[A-z\s\-,.']*$/i).test(f.value)?f.value = f.value.replace(/[^A-z\s\-,.']/ig,''):null;

    it is still allowing the ^ _ [ ] \ and any other character using alt#

    MANY THANKS!

  15. #15
    Join Date
    Jun 2013
    Posts
    14
    You could set up a bunch of regular expression that checked for link code and abort the form. E.g. a whole string of things like.
    cool things

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles