Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
i want to validate pincode is not less than 6 digit and mobile no is not less than 10 digit . so please give me suggestion .
Posted
Updated 20-Feb-16 0:24am
Comments
What have you tried and where is the problem?
Jignesh Khant 28-Dec-13 1:53am    
Try this regular expression validator:

\d{10}

Use a Regex.
Pin code:
^\d{6,}$

Mobile:
^\d{10,}$



"how to use regex, please give an example?"
"winforms"

C#
private Regex pinCode = new Regex(@"^\d{6,}$", RegexOptions.Compiled);
private Regex mobileNo = new Regex(@"^\d{10,}$", RegexOptions.Compiled);

private void MyButton_Click(object sender, EventArgs e)
    {
    string input = "123456";
    if (pinCode.IsMatch(input))
        {
        Console.WriteLine("PIN");
        }
    if (mobileNo.IsMatch(input))
        {
        Console.WriteLine("MOBILE");
        }
 
Share this answer
 
v2
Comments
An@nd Rajan10 28-Dec-13 4:40am    
how to use regex, please give an example?
OriginalGriff 28-Dec-13 4:53am    
What environment are you working in? It can make a difference!
An@nd Rajan10 28-Dec-13 5:01am    
working in C#..
OriginalGriff 28-Dec-13 5:08am    
Yes, I can tell from your tags.
Web? WinForms?
An@nd Rajan10 28-Dec-13 5:10am    
winforms
Check this out and DIY please:
Regular Expression Language - Quick Reference[^]
 
Share this answer
 
thanks for reply but i can use following code


C#
private void actxtpincode_Validating(object sender, CancelEventArgs e)
        {
            Validatefivedigits(actxtpincode);
        }
        private void Validatefivedigits(TextBox actxtpincode)
        {
            if (actxtpincode.Text.Length != 6)
            {
                
                errorProvider1.SetError(actxtpincode, "Pincode must be six digits");
            }
            else
            {
              
                errorProvider1.SetError(actxtpincode, "");
            }
        }
 
Share this answer
 
v2
Hello ,

you can use Validating Event of that particular TextBox .

and to check the no. of characters, use Length property .

try this
C#
private void txtpin_Validating(object sender, CancelEventArgs e)
        {

            if (txtpin.Text.Length > 6)
            {
                //Error Msg
            }

        }

Remember that Validating event occurs when control is validating.

you can also use TextChanged event of the Textbox . For more details on TextChanged event go through This link

thanks
 
Share this answer
 
v2
Comments
agent_kruger 28-Dec-13 3:41am    
instead of showing error you could stop the user from entering wrong values .See "Solution 5".
instead of all this, set MaxLength of the pincode textbox to 6 and mobile to 10 or 11 [11 becuase to accept 0 in begining] and on Leave Event write
if(txtPinCode.Text.Trim().Length < 6)
{
     txtPincode.Focus();
}
 
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