Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 8th, 2008, 08:56 AM
Registered User
 
Join Date: Sep 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!

 
Old September 8th, 2008, 09:18 AM
Authorized User
 
Join Date: Sep 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to peirix
Default

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.

 
Old September 8th, 2008, 10:09 AM
Registered User
 
Join Date: Sep 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply!

This is what I could come up with

<html>
<head>
<script>


function checkChar(e)
{
    var keynum;
    if(window.event) // IE
      {
          keynum = e.keyCode;
      }
    else if(e.which) // Netscape/Firefox/Opera
      {
      keynum = e.which;
      }
    if(keynum=16)
    {
        cnt=1;
    }
    if(keynum==55||keynum==61)
    {
        return false;
    }

}




</script>

</head>
<body>

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

</body>
</html>

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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript, Msxml2.XMLHTTP - Spanish Characters Pr itHighway Classic ASP Basics 1 November 14th, 2008 09:20 PM
JavaScript detect screen resolution crmpicco Javascript How-To 1 September 19th, 2005 06:07 AM
Detect dll through javascript vinodkalpaka Javascript 0 July 18th, 2005 03:34 AM
Inserting Special Characters From JavaScript richard.york Javascript 5 June 9th, 2005 01:04 PM
Javascript to HTML Special Characters gilf Javascript 6 May 19th, 2004 09:25 AM





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