p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   javascript to detect characters (http://p2p.wrox.com/showthread.php?t=70466)

sbkrohit September 8th, 2008 08:56 AM

javascript to detect characters
Hello everyone

I need to check if the user types in any special characters like $ or & in a text filed and delete them automatically as soon as it is typed in.Is this posible using javascript?or should i wait till the user clicks the submit button and perform the regular validation?

Thanks in advance!

peirix September 8th, 2008 09:18 AM

Well, you can alwasy use onkeydown="checkCharacters();", which you can then set to do the check.

Then use indexOf to find the characters and delete them.

sbkrohit September 8th, 2008 10:09 AM

Thanks for the reply!

This is what I could come up with


function checkChar(e)
    var keynum;
    if(window.event) // IE
          keynum = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
      keynum = e.which;
        return false;




<form action="">
First name:
<input type="text" name="firstname" onkeydown="return checkChar(event)">
Last name:
<input type="text" name="lastname">


The problem is that the shift press is also being treated as a event and when I click on number 7 for & ,the ascii code obtained is 55 which is for number 7 and not for & . So now 7 and & are not printing but I dont want only special characters.

Thank You

All times are GMT -4. The time now is 06:47 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.