Results 1 to 11 of 11

Thread: Javascript supress characters

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Javascript supress characters

    I have a text field and would like to stop the user from entering "/" and "\" characters.

    I dont want to validate this on submission of a form but would rather supress this as the users are typing.

    Can anyone help ?

    Parksie

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Something similar to this:
    Code:
     <input type=text name=num2 onchange="checkvalue(this,200)">
    Code:
     <script>
    <!--
    	function	checkvalue(field,limit)
    	{
    		if(field.value > limit)
    		{
    			alert("Out of range in " + field.name);
    			field.value = 0;
    		}
    	}
    -->
    </script>
    http://www.scit.wlv.ac.uk/~jphb/java...ples/ex9d.html
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    ober, ***??

    this should do it:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <body>
    <script language="JavaScript" type="text/javascript">
    <!--
    function check_for_evil_characters(str) {
    regexp=/\\/
    if (str.value.search("/") != -1)
    	 {
    	 output = str.value.substring(0,str.value.search("/"))
    	 str.value = output
    	 }
    else if (str.value.search(regexp) != -1)
    	 {
    	 output = str.value.substring(0,str.value.search(regexp))
    	 str.value = output
    	 }
    }
    //-->
    </script>
    
    <input type="text" onkeyup="check_for_evil_characters(this)">
    </body>
    </html>
    Have I helped you? Please Rate my posts.

  4. #4
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I wasn't telling him how to do it. I was giving him an example of a method.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    Acidic, ***? Sorry, couldn't help it.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <title>JavaScript Test</title>
        <script type="text/javascript">
          function myFoo(myInput) {
            myInput.value = String(myInput.value).replace(new RegExp("/\/|\\/", "g"), "");
          }
        </script>
      </head>
      <body>
        <p><input type="text" onkeyup="javascript:myFoo(this);"></p>
      </body>
    </html>
    Remove the space in JavaScript added by the forum.

    Wow... that is pretty if I do say so myself. Too bad I'm not asked to do stuff like this for a job interview.

    You still need to validate on the server side incase they disable JavaScript.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    I bow my head. I didn't think of using replace(), or an OR in regexp. donnu why. Thanks for the knock over the head.
    Have I helped you? Please Rate my posts.

  7. #7

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Wink

    Thanks a lot.

    I applaud your genius and pray that God will rainy pretty flowers on all of your heads.


    Parksie

  8. #8

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I have 2 wee problems with this.

    How can I also check for sinle and double speach marks ?
    How does javascript handle these and what is the "g" for ?

    Parksie

  9. #9
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Originally posted by venerable bede

    How does javascript handle these and what is the "g" for ?
    g stands for global, so it searches all the instances, not only the first. Not really necessary in this case.

    for speach marks you might have to search for the ASCII value of them, not quite sure what that is.
    and sinle? Which character is that?
    Have I helped you? Please Rate my posts.

  10. #10
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    True, the global flag isn't necessary, just habit. The global would be necessary if you were triggering on onBlur.

    As to the double and single quotes:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <title>JavaScript Test</title>
        <script type="text/javascript">
          function myFoo(myInput) {
            myInput.value = String(myInput.value).replace(new RegExp("['\/\"]", "g"), "");
          }
        </script>
      </head>
      <body>
        <p><input type="text" onkeyup="javascript:myFoo(this);"></p>
      </body>
    </html>
    You may notice that this isn't looking for backslashes. Seems my old code wasn't finding them, either. I haven't figured out why. Just figured I'd post this until I fixed the problem.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  11. #11
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    hmm. my old code did look for them. just add that IF satement to the ned of this script.
    Have I helped you? Please Rate my posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width