Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am working on a project in which there is a application form in which I want to enter only characters(a-z,A-Z). Numbers and special characters are not allowed. White space is allowed.If number is entered it should not display in text-box so anyone can help me.??
Posted
Updated 27-Apr-18 2:00am
v3
Comments
OriginalGriff 2-Apr-13 4:14am    
Is this web or Winforms? The solution is different depending on where you are trying to run.
rgboss 2-Apr-13 7:06am    
It is a web form....

This article shows how this can be done. In fact there is a reusable js file that can be included and used with any project to achieve this and many other common validation and filtration scenarios.

A Tiny Javascript Framework for Common Validation Scenarios.[^]
 
Share this answer
 
Comments
rgboss 4-Apr-13 1:33am    
Thanks Rahul.It was really helpful.
There are a number of ways to do this.
You can use regex patterns, validator controls or just plain key down handling to achieve these results.

Simple key event handling example -
C#
private void txtInputNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsCharacter(e.KeyChar) || char.IsSpecial(e.KeyChar);

        }


The IsCharacter and IsSpecial methods check if the input character is either of the two.
 
Share this answer
 
You can use regular expression to validate the entered text in javascript.
Hope these links may help you
http://www.advanced-javascript-tutorial.com/RegularExpressions.cfm#.UVqS10O6a1s[^]

your regular expression can be like this
var reSSN = /^[A-Za-z0-9 ]$/;
 
Share this answer
 
v2
Try this
C#
<script type="text/javascript">
        function Validate(event) {
            var regex = new RegExp("^[0-9a-zA-Z]+$");
            var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
            if (!regex.test(key)) {
                event.preventDefault();
                return false;
            }
        }       
    </script> 
<asp:textbox id="txtDemo" onkeypress="return Validate(event);" runat="server" xmlns:asp="#unknown" />
 
Share this answer
 
You can use FilteredTextBox extender is you are using ASP.net Webform application.

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx[^]
 
Share this answer
 
Comments
rgboss 2-Apr-13 7:12am    
Thanks Vijay...

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900