Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > restrict the user entering nonalphanumeric keys?

Reply
Thread Tools

restrict the user entering nonalphanumeric keys?

 
 
=?Utf-8?B?Qw==?=
Guest
Posts: n/a
 
      06-13-2007
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.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      06-13-2007
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.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Entering strings as user input but interpreting as Python input (sortof) Chris Carlen Python 1 09-18-2007 01:58 AM
Restrict user if user directly types url of any page Jaydeep ASP .Net 3 06-30-2006 04:40 AM
Prevent user from entering punctuation PW HTML 10 06-18-2006 01:10 PM
IOS user authentication when entering enable mode MayaShani@gmail.com Cisco 3 03-13-2006 07:07 AM
How do i restrict entering non-numeric data =?Utf-8?B?c3A=?= ASP .Net 2 10-27-2004 06:09 PM



Advertisments