Validate that the phone number only contains only numbers [duplicate] - c#

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Identify if a string is a number
(26 answers)
Closed 8 years ago.
I need to do a phone number validation that the tel# contains only:
numbers 0-9
left and right parenthesis
hyphen
blank space
in c#
this is my code for now:
//ValidatePhoneNumber
public static string ValidatePhoneNumber(string v_strPhoneNumber)
{
const string strPHONE_NUMBER_BLANK =
"Phone Number cannot be blank";
const string strPHONE_NUMBER_TOO_LONG =
"Phone Number cannot be longer than 24 characters";
if (v_strPhoneNumber.Trim().Length == 0)
return strPHONE_NUMBER_BLANK;
if (v_strPhoneNumber.Trim().Length > 24)
return trPHONE_NUMBER_TOO_LONG;
return String.Empty;
}//end ValidatePhoneNumber
I need to add that validation into that code, I am a beginer so the easiest way possible would be great. If you can give me the answer please if you could at least point me in the right direction, Thanks!

This the RegEx that I use for telephone number validation
^((| |)\d{3}()| |)(-| )\d{3}-\d{4}$
You just need to adjust it to your needs.

Related

How do I check if the user input is strictly numbers, using the bool type? [duplicate]

This question already has answers here:
Identify if a string is a number
(26 answers)
Closed 5 years ago.
I defined a string data type. Prompted the user to enter 10 numbers. e.g Console.Write("Enter your cell number: ");
I want to know how to validate the string and make sure the user entered numbers only
Best option would be use regular expression to much exactly 10 digits:
Regex pattern = new Regex(#"^\d{10}$");
if(pattern.isMatch(input))
{
//Do something
return true;
}
Another option is to use Int64.TryParse, read more here, but that additional checks are needed, to verify that result had 10 digits (no more, no less), number is not negative etc.
bool result = Int64.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}

Validate a string based on certain Format

I need to validate a input string based on certain formats i.e
Proj-######## (4 alphabets, 1 Dash and 8 numbers)
OP###### (2 characters, 6 numbers)
Can someone please help me on this?
I tried with the below approach it's working for 1 dash and 8 numbers. but am not geeting how to add code into regerx for allow only 4 charactes.
private static readonly Regex boxNumberRegex = new Regex(#"^\d-\d{8}$");
public static bool VerifyBoxNumber (string boxNumber)
{
return boxNumberRegex.IsMatch(boxNumber);
}
Try this.
\b[a-zA-Z]{4}-\d{6}\b - is for Proj-########
\b[a-zA-Z]{2}\d{6}\b - is for OP######
If you want to learn bulding regular expressions, have a look at this article. Worth reading it.
http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial

How to validate a string so that it's not empty and contains numeric values? [duplicate]

This question already has answers here:
Identify if a string is a number
(26 answers)
Closed 8 years ago.
As I am making a console application I can't seem to get the .Length to check whether it's empty for an if statement to work, says that it's "read only".
I can't simply define the value as int due to using console to retrieve the data.
Console.Write("Enter your Phone Number: ");
phone_number = Console.ReadLine();
You can use int.TryParse(). It returns true if the string is a valid int. If the string is empty or not numeric it will return false. You didn't mention it in your question so I am assuming you aren't entering in decimal numbers (ie. 34.12).
int phoneNumber;
if(int.TryParse(Console.ReadLine(), out phoneNumber))
{
// phone number was an int
}
else
{
// not an int
}
Although not exactly your question, you should use only the digits of your input as a phone number. The format may vary but still be valid:
(+001)555-123456
555 123456
123456
555 12 34 56
All valid answers to the question. Don't make people type their number in ways they never do it. Just accept their input and take what you need:
Console.Write("Enter your Phone Number: ");
var input = Console.ReadLine();
string phoneNumber = new string(input.Where(char.IsDigit).ToArray());
That's very simple.
first you check if the string is null or empty using this Method:
String.IsNullOrEmpty(string value)
if the method returned true it means that you should not continue(the string is null or empty).
then you want to check that if the string can be converted to int(number).so you can use:
public static bool TryParse(
string s,
out int result)
If you want to know more about int.TryParse method,you can refer to http://www.dotnetperls.com/int-tryparse for a simple tutorial.
Happy codding. If you have anymore answers,I'm ready here :D

check the string having full number or the combinbation of number and string [duplicate]

This question already has answers here:
Regex for numbers only
(20 answers)
Closed 9 years ago.
I want to check whether a string is only numeric, or is alphanumeric.
For example :
string test = "2323212343243423333";
string test1 = "34323df23233232323e";
I want to check test having number only or not. If the whole string having number means it returns true. Otherwise it returns false.
How can i do this?
bool allDigits = text.All(c => char.IsDigit(c));
Or
bool allDigits = text.All(char.IsDigit);
Unless by "numeric" you include hex numbers? My answer only works for strings that contain only digits, of course.
if the string length is not too long, you may try int.TryParse(string here) or you may write the function yourself by checking every character in the string like
if(MyString[i]-'0'<= 9 && MyString[i]-'0'>= 0)
//then it's a digit, and check other characters this way

Add Thousand Separator To A Double Value(Control Decimals, Remove Zero Decimals) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
i have a double value that i want to add it thousand separator, with below conditions :
1- remove zero decimals after dor(.)
2-control count of decimals
for example :
string str_Money = Convert.ToDouble(Money).ToString("N3");
this code for Money = 50000 returns 50,000.000, but i don't want zero decimals(mean i want 50,000)
another example : for Money = 50000.2355 returns 50,000.235 and that is exactly what i want
how can i reformat it?
Using the accepted answer from .NET String.Format() to add commas in thousands place for a number, use an if statement to control to returned format.
string str_Money = "";
if (money % != 0) // test for decimals
{
str_Money = string.Format("{0:n0}", money); // no decimals.
}
else
{
str_Money = string.Format("{0:n}", money);
}

Categories