473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Custom Keystroke Script Help

I am kinda confused at the moment. I want the users to only be able to enter alpha/numeric characters, but be able to use the spacebar as well. The Regex I have right now works, but does not allow spaces to be entered. I can't figure out how to allow for spaces. I've tried all I can think of right now, but nothing seems to work right. Here is the working code without spaces......
Expand|Select|Wrap|Line Numbers
  1. //Allow only alpha/numeric keystrokes
  2. if (event.change.match(/(\W+)/g)) 
  3.     event.rc = false;
  4.  
Another question, not that it's important or anything, why do I have to use \W instead of \w for alpha/numeric characters? Anyways, any help would be appreciated........
Sep 18 '07 #1
8 2793
pbmods
5,821 Expert 4TB
Heya, Norman. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]
Sep 18 '07 #2
acoder
16,027 Expert Mod 8TB
I am kinda confused at the moment. I want the users to only be able to enter alpha/numeric characters, but be able to use the spacebar as well. The Regex I have right now works, but does not allow spaces to be entered...
You can use \s for whitespace characters. This would match tabs and line feeds too.
Another question, not that it's important or anything, why do I have to use \W instead of \w for alpha/numeric characters?
\W is a negated character class. See link. You're trying to match a non-alphanumeric character.

A note: \w also matches underscore.
Sep 18 '07 #3
Ya, I know the \w is for alpha/numeric, but when I do that, all I can do is use special characters. No numbers or alphabetic characters at all. Which I thought was weird. The problem I'm having is placing the \s without "voiding" the script. Everywhere I've tried to place the \s makes the entire string act like there's nothing there and you can then enter whatever you want in the field. I just can't figure this one out. Any new scripts to only allow alpha/numeric keystrokes wouldn't hurt any. I have it working for numeric and alpha only characters, just not alpha/numeric. Appreciate the help though.....
Sep 18 '07 #4
mrhoo
428 256MB
Use a space character to match a space in a regular expression.

var Rx= /[a-zA-Z0-9 ]+/ will match any number of letters, digits and spaces.




Note to moderators- this is a comment, not a script. It is easier to read and understand without your code formatting. The next time you edit one of my posts will be my last post in this forum.
Sep 18 '07 #5
Still not workin' for me. Thanks for the help guys....guess I'll just keep playing around with it or just not be able to format it the way I want. Life will go on. Thanks for tryin'
Sep 18 '07 #6
acoder
16,027 Expert Mod 8TB
Ya, I know the \w is for alpha/numeric, but when I do that, all I can do is use special characters. No numbers or alphabetic characters at all. Which I thought was weird. The problem I'm having is placing the \s without "voiding" the script. Everywhere I've tried to place the \s makes the entire string act like there's nothing there and you can then enter whatever you want in the field. I just can't figure this one out. Any new scripts to only allow alpha/numeric keystrokes wouldn't hurt any. I have it working for numeric and alpha only characters, just not alpha/numeric. Appreciate the help though.....
The reason for this is that you're matching the opposite of what you want to allow and cancelling the event. Use the regex posted by mrhoo, but add a !, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (!event.change.match...)
Sep 18 '07 #7
The reason for this is that you're matching the opposite of what you want to allow and cancelling the event. Use the regex posted by mrhoo, but add a !, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (!event.change.match...)
Thank you! That worked great, except now you can't backspace or delete the input. Any suggestions on how to allow the backspace?



Edit:
I basically did the opposite and got a satisfying result....

Expand|Select|Wrap|Line Numbers
  1. //Allow only alpha/numeric keystrokes with spaces
  2. if (event.change.match(/[!@#$%^&*()=+?><"~]+/))
  3.     event.rc = false;
  4.  
Thank you so much guys for your help!!!
Sep 18 '07 #8
acoder
16,027 Expert Mod 8TB
Thank you! That worked great, except now you can't backspace or delete the input. Any suggestions on how to allow the backspace?
Use \b to match a backspace.
Edit:
I basically did the opposite and got a satisfying result....

Expand|Select|Wrap|Line Numbers
  1. //Allow only alpha/numeric keystrokes with spaces
  2. if (event.change.match(/[!@#$%^&*()=+?><"~]+/))
  3.     event.rc = false;
  4.  
This will not work properly if someone uses special characters with accents and umlauts.
Sep 19 '07 #9

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

Similar topics

7
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...
2
by: satish | last post by:
Hello all, I have a shared object executable viz. *cable* which I execute as follows : $ ansyscust71 -custom cable -p ANSYSRF **ansyscust71 is a shell script and is a part of a software...
1
by: Amin Gunawan via .NET 247 | last post by:
I would like to put a combo box in a column of a datagrid. I've seen the samples in the MSDN library of other sources, butall samples didn't catch the 'DOWN/UP' keystroke. It means thatwhen I select...
2
by: Alani | last post by:
Hello All, I'm a new ASP.NET programmer and I want to create a custom control consist of two properties (Number1) and (Number2) and both of them are integers and default value = 0, now I'm...
2
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...
13
by: Matt F | last post by:
I have a deployment project that I'm setting up. I need to perform a different custom action based on whether this is a first time install or an update. Does anyone have any idea if it's possible...
0
by: bobneedshelp | last post by:
Is it possible through an install to run a script to disable a service and then install the files in VB.NET? I've tried using an installer class with the custom actions and that is not working....
2
by: nussbac | last post by:
Hi I suspect this is not possible, and perhaps someone will explain why, but does anyone know how to capture the 'PrintScreen' keystroke (and/or the 'Ctrl-PrintScreen' event)? I would like to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.