Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Do anyone have an idea about the following scenario.
I have a datagridview in which I've handled keypress event for the first datagridviewtextboxcolumn. In my keypress I've allowed characters, digits but no spaces. How to not allow users to give number as first input For eg they cant give
1abc as input but can give as abc123. How to achieve.pls do assist me further


Thanks in advance
Posted
Comments
demouser743 30-Dec-11 3:32am    
Can you write down all possible conditions.. Is this valid abc123abc
RaviRanjanKr 30-Dec-11 18:36pm    
Not clear! please post your codes.

VB
Private Sub txtValue2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue2.KeyPress
        If ((AscW(e.KeyChar) > 47 And AscW(e.KeyChar) < 58) Or (AscW(e.KeyChar) = 8) Or (AscW(e.KeyChar) = 46)) Then
            e.Handled = False
        Else
            e.Handled = True
            MsgBox("Only numeric charecter allowed")
        End If
    End Sub



This code is an example follow this and do as you want.

happy coding
 
Share this answer
 
v2
Comments
CyborgForever 30-Dec-11 1:24am    
I've already handled the keypress event for datagridviewtextboxcolumn, but I've a condition the first time user gives input he cant enter a number only char, after entering char he can give numbers how to achieve this...
[no name] 30-Dec-11 6:27am    
You can do this by extending the example you received from biswarzp88.

if (txtValue2.Text.Length() > 0)...else...

Instead of comparing induvidual you can also use char.IsLetter(..), char.IsNumber(...)

For me the answer needs 5.Regards
I tried this in button click try as per you requirement

Add this regex and make a try [A-Za-z]\w*
C#
string strText = textBox2.Text;
 Regex reg = new Regex("^[A-Za-z]+\\d{0,9}$");
 if (reg.IsMatch(textBox2.Text))
 {
 }
 else
 {
    MessageBox.Show("Invalid");
 }


or you can write some thing like this

C#
if(textBox2.length>1)

    {
    txt = textBox2.substring(0,1);
    if(!((txt >= 'a' && txt <= 'z') || (txt >= 'A' && txt <= 'Z')))
    }


C#
private void button1_Click(object sender, EventArgs e)
        {
            string strText = textBox2.Text;
            Regex reg = new Regex("^[A-Za-z]+\\w*");
            if (reg.IsMatch(textBox2.Text))
            {
                MessageBox.Show("Valid");
            }
            else
            {
                MessageBox.Show("Invalid");
            }
        }
 
Share this answer
 
v3
Comments
CyborgForever 30-Dec-11 5:29am    
this allow only characters right both char n no should be allowed, but first one should not be a number, after entering characters numbers can be entered.
demouser743 30-Dec-11 5:33am    
Try with my new Regex that i updated
CyborgForever 30-Dec-11 5:49am    
I used the exactly the same regex which u have updated it says unrecognised escape sequence.
So I used like this Regex reg = new Regex("^[A-Za-z]+\\w*"); but it allows only character but not numbers
demouser743 30-Dec-11 6:04am    
I don't know where you went wrong the same expression working for me check the images http://i.stack.imgur.com/9guXG.jpg http://i.stack.imgur.com/0XwoJ.jpg
demouser743 30-Dec-11 6:05am    
Check my updates and try once

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