www.webdeveloper.com
+ Reply to Thread
Results 1 to 13 of 13

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,427
    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,427
    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,427
    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,764
    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.
    Great wit and madness are near allied, and fine a line their bounds divide.

  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,427
    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-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;

  11. #11
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,427
    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.

Thread Information

Users Browsing this Thread

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

     

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
HTML5 Development Center



Recent Articles