Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi friends,

In my application having morethan 5 textboxes .In this textbox i want to put one validate control whenever enter in Name textboxes in values it automatically show error message like please enter alphabets.
Problem: only Accept alphabets does nt accepts in Numbers .please help me.........

Thanks in Advance
Sheshu
Posted
Comments
Balamurugan1989 31-Jan-13 4:18am    
R u working in windows or web???
kayalasheshu 31-Jan-13 4:25am    
web but i need both if u knw......
Balamurugan1989 31-Jan-13 4:41am    
If it works for u accept my solution...
Balamurugan1989 31-Jan-13 4:31am    
For windows i have updated the answer below it will useful for u...
kayalasheshu 31-Jan-13 6:45am    
i need c# code not in VB

Hi kayalasheshu,

Lucky for you, MS has an off-the-shelf validator controls.

See this[^] article doing a nice validator controls review.

You can out this nice CodeProject article[^]

Cheers,
Edo
 
Share this answer
 
v2
use javascript fuction to check it onBlur event
 
Share this answer
 
You will find this useful.

A Tiny Javascript Framework for Common Validation Scenarios.[^]

although this link is currently disallowing the input, you can change it to show errors too.
 
Share this answer
 
If you are working in Windows application follow the below steps it will work,
1.Right Click Textbox
2.Go to Properties
3.Click the Event image in the properties window.
4.Double Click KeyPress in the textbox.
5.It will pass to Code Page,in the code page u need to write the below coding
C#
if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || e.KeyChar == 32 || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else
            {
                MessageBox.Show("Only Character");
                e.Handled = true;
            }


Surely this will work try it....
 
Share this answer
 
if you use web based then use below function

C#
public bool IsAlpha(string input)
{
    return Regex.IsMatch(input, "^[a-zA-Z]+$");
}
public bool IsAlphaNumeric(string input)
{
    return Regex.IsMatch(input, "^[a-zA-Z0-9]+$");
}

public bool IsAlphaNumericWithUnderscore(string input)
{
    return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$");
}

or if you use Web development then use java script

XML
script type="text/javascript" language="javascript">
    function validateNumbersOnly(e) {
        var unicode = e.charCode ? e.charCode : e.keyCode;
        if ((unicode == 8) || (unicode == 9) || (unicode > 47 && unicode < 58)) {
            return true;
        }
        else {

            window.alert("This field accepts only Numbers");
            return false;
        }
    }
</script>



on page use below code
XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server" Display="None" ErrorMessage="Accepts only numbers." ControlToValidate="TextBox1" ValidationExpression=""^[0-9]*$" Text="*"></asp:RegularExpressionValidator>
 
Share this answer
 

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



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