473,398 Members | 2,404 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,398 software developers and data experts.

restrict the user entering nonalphanumeric keys?

I want to restrict the user entering nonalphanumeric keys using Javascript
onKeypress.
One exception is that I want to allow the user to enter spaces.

How can I do this?

Thanks in Advance.

Jun 13 '07 #1
1 1909
Howdy,

Two variants (both restrict to alphanumeric or nonalphanumeric):
<asp:TextBox runat="server"
onKeyPress="return restrict(this, true);" ID="TextBox1" />
<asp:TextBox runat="server"
onKeyPress="return restrict(this, false);" ID="TextBox2" />

<script type="text/javascript">
function restrict(input, alphanumeric)
{
var regex = alphanumeric ? /[a-zA-Z0-9_ ]/ : /[^a-zA-Z0-9_ ]/;

var code = event.which ? event.which : event.keyCode;

return regex.test(String.fromCharCode(code));
}
</script>

hope this helps

--
Milosz
"C" wrote:
I want to restrict the user entering nonalphanumeric keys using Javascript
onKeypress.
One exception is that I want to allow the user to enter spaces.

How can I do this?

Thanks in Advance.
Jun 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: vijay21 | last post by:
hi all, Can we restrict the number of characters that the user can input in a javascript prompt? my browser hangs whenever i try to input more than 2048 characters in it. Thanks for any help
2
by: sp | last post by:
Hello Everybody, How can I restrict the user entering non-numeric data in textbox control on server side using asp.net. Any Code sample or suggestions are appreciated. Thanks in advance sp
10
by: sconeek | last post by:
hi all, i have a textfield where i would like the user to input only Y or N. can somebody tell me how can i restrict the user from entering any other character, number or special character....
2
by: Tarkeshwar | last post by:
Hi All, I want to restrict the user from being entering the value in <input type="file">. It works fine in IE but not in Mozilla. I am sending my code also which works in IE and not in Mozilla.Can...
2
by: den 2005 | last post by:
Hi everybody, How do restrict entering these characters <>\"%';()& and telling user these caharcters are not allowed to be enter in the textbox field using RegularExpression Validator? I put in...
3
by: divaof_7 | last post by:
I've a textbox field, where user enters his name. I want to restrict him from entering double and singls inverted commas.What kind of function should i write.
5
by: SKS | last post by:
hi all i would like to know how to restrict user from entering numeric value ina textbox..using C# if user try to enter text it shouldnt be allowed.. i tried errror provider.. but it doesnt met...
1
by: Albin | last post by:
Hi, I have a form in which i have two text boxes one for entering email and the other for verify email (to make sure the user entered the correct email id we are asking to enter the same email...
4
by: vinpkl | last post by:
hi i am working on admin section which has a login page with login id and pasword form. in my admin section i have many pages say like manage_products.php, description.php, user.php etc. if i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.