SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    abort user from typing certain chararcters

    Case:
    Suppose i have a input text field which is supposed to take only numbers ie 0-9
    If the user types any alphabets or any non numeric no then Javascript
    should display the alert at the time and writing and then any written alphabets or any non numeric no should be deleted.
    I mean to say is that suppose i am typing 123 and then try to write `q` then at that instant JS
    should give alert and delete that written `q`.
    Its very challenging for me...But Hope I will get my problem solved ...How ??
    Obviously from this forum...hehehe............
    Thanks in advance to all of you !!

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try a onkeypress event, look at the keycode / charcode variables, compare them with the asciitable and you should have all the ingredients to cook your code.

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Blocking keystrokes, let alone alert is extremely user-unfriendly. You'd be better off with less obtrusive error signalling, e.g.

    Code:
    <input onkeyup="checkNum(this)">
    <script>
    function checkNum(input) {
    	if(input.value.match(/\D/))
    		input.style.backgroundColor = "red";
    	else
    		input.style.backgroundColor = "";
    }
    </script>


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
  •