Click here to Skip to main content
14,699,232 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Regex(@"^[0-9]{10}$");


What I have tried:

not working, i also want to check if it is less then 10 digit then give error
Posted
Updated 3-Apr-20 1:06am
Comments
Patrice T 3-Apr-18 12:10pm
   
"not working it is giving error"
Which error ?
Show code that we can run.

try {
	if (Regex.IsMatch(SubjectString, "\\A[0-9]{10}\\z")) {
		// This is a FULL MATCH
	} else {
		// Nope, no match, do your worst
	} 
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}
   
Comments
Member 11776570 3-Apr-18 12:07pm
   
not working it is giving error, i have type mobile number in textbox.
Christiaan van Bergen 3-Apr-18 12:09pm
   
Show me the number you want to check.
Member 11776570 3-Apr-18 12:15pm
   
9876543214. this number i want to check but it shuold also check that it should be equal to 10 digits mendatory.
Christiaan van Bergen 3-Apr-18 12:18pm
   
Right, this number checks out. It IS 10 digits. Make it nine digits and it will fail....of course. Could it be that you have some spaces around your input? Try a .Trim() statement on your input.
Member 11776570 3-Apr-18 12:23pm
   
not working it is check 9 digit only not 10 digit. i want to check 10 digit mobile no like this 9876543201
Your regex works fine to check your sample data:
Regex reg = new Regex(@"^[0-9]{10}$");
string inputOK = "9876543214";
string inputBad1 = "876543214";
string inputBad2 = "09876543214";
if (reg.IsMatch(inputOK)) Console.WriteLine("OK");
if (!reg.IsMatch(inputBad1)) Console.WriteLine("OK");
if (!reg.IsMatch(inputBad2)) Console.WriteLine("OK");
It gives
OK
OK
OK
So ... the data you are checking is not what you think it is.
Use the debugger to check exactly what is in your input string when you validate it.
   
If you would like to check if mobile phone number is valid (10 digits), try this:
string[] mobilephones = {"9876543214", "987654321", "98765432142", "A876543214", "87C54d2142"};

//pattern
string pattern = @"^\d{10}$";

var result = mobilephones
	.Select(x=>new
	{
		mobile = x,
		IsValid = Regex.IsMatch(x, pattern),
	})
	.ToList();
	
foreach(var mb in result)
{
	Console.WriteLine("{0} - {1} valid mobile number", mb.mobile, mb.IsValid ? "is" : "is NOT");
}


Result:
9876543214 - is valid mobile number
987654321 - is NOT valid mobile number
98765432142 - is NOT valid mobile number
A876543214 - is NOT valid mobile number
87C54d2142 - is NOT valid mobile number


For further details, please see: Regex.IsMatch Method[^]
   
v2
Regex(@"^[0-9]{10}$");

use this it will work properly. it will also check if any alphabets are there or not. in this we can only enter 10 digits number
   
val UserMobile = findViewById<edittext>(R.id.UserMobile)
val msgUserMobile: String = UserMobile.text.toString()

fun String.isMobileValid(): Boolean {
// 11 digit number start with 011 or 010 or 015 or 012
// then [0-9]{8} any numbers from 0 to 9 with length 8 numbers
if(Pattern.matches("(011|012|010|015)[0-9]{8}", msgUserMobile)) {
return true

}
return false
}
if(msgUserMobile.trim().length==11&& msgUserMobile.isMobileValid())
{//pass} else {//not valid}
   

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