473,618 Members | 3,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preventing a keystroke value from being added to string.

Claus Mygind
571 Contributor
with the "onKeyPress " event handler I see that I can capture and examine the value of a keystroke before it is displayed on the screen. I have made good use of this for single character fields where I want to examine if the value is correct and also convert the value to upper case like so:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <input 
  3.    type="text" 
  4.    name="f1" 
  5.    id  ="f1"  
  6.    maxLength="1"
  7.    onKeyPress="return isHorS(event);" 
  8.  />
  9.  
  10. function isHorS(evt)
  11. {
  12.    evt = (evt) ? evt : event; 
  13.    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));   
  14.  
  15.       if (charCode != 8 && charCode != 9 && charCode != 32 && charCode != 72 && charCode != 104 && charCode != 83 && charCode != 115 )
  16.       {  
  17.             alert("You must either leave blank or enter H or S!"); 
  18.              document.getElementById("f1").value = "";
  19.       }else{
  20.           if (charCode == 104  || charCode == 115 )
  21.           {
  22.             var cUp = String.fromCharCode(charCode)
  23.             document.getElementById("f1").value = cUp.toUpperCase();
  24.           }
  25.       }
  26.  
  27. }
  28.  
As I said this works great when I am only dealing with one character. But for longer strings than one character, I would like to prevent unwanted characters from becoming part of the string. Can I use this technique by intercepting what the user is typing give them an alert and then cancel that keystroke entry?
Aug 28 '08 #1
3 1745
Logician
210 New Member
It's much simpler to use a regular expression to remove unwanted characters from the string after each keystroke. Use the onkeyup event.
Aug 28 '08 #2
Claus Mygind
571 Contributor
great do you have an example I can use. although I did get it to work like this:
Expand|Select|Wrap|Line Numbers
  1.  onKeyPress=" return isValidCode(event, this);"
  2.  
  3. function isValidCode(evt, obj)
  4. {
  5.    evt = (evt) ? evt : event; 
  6.    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :  
  7.        ((evt.which) ? evt.which : 0));   
  8.     if ( charCode == 8 || 
  9.          charCode == 9 || 
  10.         (charCode > 47 && charCode < 58) || 
  11.         (charCode > 64 && charCode < 91) ||    
  12.         (charCode > 96 && charCode < 123) )
  13.     {
  14.         if (charCode > 96 && charCode < 123)
  15.         {
  16.             Here I would like to make it an uppercase value
  17.         }
  18.     }else{
  19.         alert("That is not a valid character ");
  20.         return false;
  21.     }
  22. }
  23.  
  24.  
Perhaps your example of a regular expression can also convert the keystroke upper case if allowed?
Aug 28 '08 #3
Claus Mygind
571 Contributor
Ok I have learned that only IE let's you intercept a keystroke and alter it before it reaches the form (generally considered a security risk as programmer can alter what the user is entering)

Expand|Select|Wrap|Line Numbers
  1. var charCode = event.keyCode;
  2.  
  3. event.keyCode = charCode - 32
And as I am working exclusively with fireFox that will not work for me.

will look at the keyup option
Aug 28 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
6219
by: hokieghal99 | last post by:
Does anyone know of a keystroke logger that has been written in Python for Windows machines? I'd like to see such a script and use it as a point of reference for a real-time backup project that I'm working on. Basically, I'd like to be able to capture all keystrokes from the keyboard buffer and write them to a text file so I could reporduce emails, documents, etc. in the event of file loss that occurs between nightly backups. For example,...
2
3232
by: Wayne Wengert | last post by:
I want to get the decimal (integer) value of user keystrokes. I am using the following code. chrInput is the character entered (e.g. "M") and "keystroke is DIMed as Short keystroke = Microsoft.VisualBasic.Val(chrInput) but it always yields a value of 0 How should I be doing this?
8
2331
by: CJM | last post by:
How do people go about preventing the user from submitting a form for a 2nd time? For example, the user submits a form, clicks on the back button, and the submits the form again. I have used various techniques in the past (depending on circumstances) but I'd be interested in the techniques you guys currently use. Thanks --
8
2433
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it was the entire function failing, but I tested with alerts and found that it was only the 'window.open' that fails to execute. The function is being called by a link, and I suspected some problem with the body alink/vlink but after cutting that out...
15
3218
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone tell me why these DIVs don't drift to the left as they are supposed to? <script language="javascript">
3
2165
by: ColinWard | last post by:
I am using the following code to validate that the person that is being entered into the database does not already exist. However wnem I test it by entering myself as a contact(I first checked that I was indeed NOT in the database), the message still comes up saying that I am in the database. what am I doing wrong? Private Sub txtEmailName_AfterUpdate() On Error GoTo Err_txtEmailName_AfterUpdate >> If DLookup("EmailName", "Contacts",...
10
4499
by: et | last post by:
I have an asp.net program that uses a connection string, using integrated security to connect to a sql database. It runs fine on one server, but the other server gives me the error that "Login failed for user "NT AUTHORITY/ANONYMOUS LOGON". Why would this be? There is no reason it should even be trying to login to using NT Authority/Anonymous login. The IIS Server is set to turn off anonymous logins, and use integrated security, and my...
3
1483
by: Linda | last post by:
Greetings! In order to keep better track a form with several combo-boxes, I've subclassed the ones that need to change in response to events on other controls on the form. An example of a subclassed combo box: <code> Public Class byte13ComboBox Inherits System.Windows.Forms.ComboBox Private comboItems() As String = {"a (Numbered)", _
2
1329
by: John | last post by:
We have an interesting programming problem and I wonder if anyone would know how to accomplish this, or be able to point me in the right direction I have a list of 100 items, in this case it's names and numbers. The entire list is written to one data grid, or table in an ASPX page As having to scroll through that list is already inefficient, and more names will be added, I need to find an elegant solution that will let me get to the...
0
8207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8650
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8593
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7124
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6098
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4064
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2582
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.