Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 19: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
0 OriginalGriff 248
1 Maciej Los 210
2 Sergey Alexandrovich Kryukov 160
3 Peter Leow 155
4 George Jonsson 135


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