Click here to Skip to main content
12,697,801 members (26,773 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
I am developing an application on ASP.NET in C# where I need to pass only text and numbers. Please help me to do this.
Thanks in advance.
Posted 13-Nov-12 20:17pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try the following

<asp:TextBox ID="txtCode" runat="server" Width="200px"                         Enabled="true" OnKeyPress="return restrictCharacters(event);"></asp:TextBox>


and the below JavaScript function

function restrictCharacters(e) {
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    var alphaOnly = /[A-Za-z0-9]/g;
    if (code == 27) { this.blur(); return false; }
    if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40) {
        if (character.match(alphaOnly)) {
            return true;
        } else {
            return false;
        }
    }
}


Hope this helps.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100