Best practice for parsing and validating mobile number - c#

I wonder what the best practice for parsing and validating a mobile number before sending a text is. I've got code that works, but I'd like to find out better ways of doing it (as my last question, this is part of my early new years resolution to write better quality code!).
At the moment we are very forgiving when the user enters the number on the form, they can enter things like "+44 123 4567890", "00441234567890", "0123456789", "+44(0)123456789", "012-345-6789" or even "haven't got a phone".
However, to send the text the format must be 44xxxxxxxxxx (this is for UK mobiles only), so we need to parse it and validate it before we can send. Below is the code that I have for now (C#, asp.net), it would be great if anyone had any ideas on how to improve it.
Thanks,
Annelie
private bool IsMobileNumberValid(string mobileNumber)
{
// parse the number
_mobileNumber = ParsedMobileNumber(mobileNumber);
// check if it's the right length
if (_mobileNumber.Length != 12)
{
return false;
}
// check if it contains non-numeric characters
if(!Regex.IsMatch(_mobileNumber, #"^[-+]?[0-9]*\.?[0-9]+$"))
{
return false;
}
return true;
}
private string ParsedMobileNumber(string number)
{
number = number.Replace("+", "");
number = number.Replace(".", "");
number = number.Replace(" ", "");
number = number.Replace("-", "");
number = number.Replace("/", "");
number = number.Replace("(", "");
number = number.Replace(")", "");
number = number.Trim(new char[] { '0' });
if (!number.StartsWith("44"))
{
number = "44" + number;
}
return number;
}
EDIT
Here's what I ended up with:
private bool IsMobileNumberValid(string mobileNumber)
{
// remove all non-numeric characters
_mobileNumber = CleanNumber(mobileNumber);
// trim any leading zeros
_mobileNumber = _mobileNumber.TrimStart(new char[] { '0' });
// check for this in case they've entered 44 (0)xxxxxxxxx or similar
if (_mobileNumber.StartsWith("440"))
{
_mobileNumber = _mobileNumber.Remove(2, 1);
}
// add country code if they haven't entered it
if (!_mobileNumber.StartsWith("44"))
{
_mobileNumber = "44" + _mobileNumber;
}
// check if it's the right length
if (_mobileNumber.Length != 12)
{
return false;
}
return true;
}
private string CleanNumber(string phone)
{
Regex digitsOnly = new Regex(#"[^\d]");
return digitsOnly.Replace(phone, "");
}

Use a regular expression to remove any non-numeric characters instead of trying to guess how a person will enter their number - this will remove all your Replace() and Trim() methods, unless you really need to trim a leading zero.
string CleanPhone(string phone)
{
Regex digitsOnly = new Regex(#"[^\d]");
return digitsOnly.Replace(phone, "");
}
Alternatively, I would recommend you use a masked textbox to collect the # (there are many options available) to allow only numeric input, and display the input with whatever format you'd like. This way you're guaranteeing that the value received will be all numeric characters.

Check out QAS, it's a commercial solution.
They have email, phone and address validations.
http://www.qas.com/phone-number-validation-web-service.htm
We use their services for Address and Email (not phone) and have been satisfied with it.

#annelie maybe you can update your regular expression to a more powerful one. Check out this site here. It contains many expressions but I think one of the top 2 expressions in the site should be suitable to you.

public class PhoneNumber
{
public PhoneNumber(string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("numberString", Properties.Resources.PhoneNumberIsNullOrEmpty);
var match = new Regex(#"\+(\w+) \((\w+)\) (\w+)", RegexOptions.Compiled).Match(value);
if (match.Success)
{
ushort countryCode = 0;
ushort localCode = 0;
int number = 0;
if (UInt16.TryParse(match.Result("$1"), out countryCode) &&
UInt16.TryParse(match.Result("$2"), out localCode) &&
Int32.TryParse(match.Result("$3"), out number))
{
this.CountryCode = countryCode;
this.LocalCode = localCode;
this.Number = number;
}
}
else
{
throw new ArgumentNullException("numberString", Properties.Resources.PhoneNumberInvalid);
}
}
public PhoneNumber(int countryCode, int localCode, int number)
{
if (countryCode == 0)
throw new ArgumentOutOfRangeException("countryCode", Properties.Resources.PhoneNumberIsNullOrEmpty);
else if (localCode == 0)
throw new ArgumentOutOfRangeException("localCode", Properties.Resources.PhoneNumberIsNullOrEmpty);
else if (number == 0)
throw new ArgumentOutOfRangeException("number", Properties.Resources.PhoneNumberIsNullOrEmpty);
this.CountryCode = countryCode;
this.LocalCode = localCode;
this.Number = number;
}
public int CountryCode { get; set; }
public int LocalCode { get; set; }
public int Number { get; set; }
public override string ToString()
{
return String.Format(System.Globalization.CultureInfo.CurrentCulture, "+{0} ({1}) {2}", CountryCode, LocalCode, Number);
}
public static bool Validate(string value)
{
return new Regex(#"\+\w+ \(\w+\) \w+", RegexOptions.Compiled).IsMatch(value);
}
public static bool Validate(string countryCode, string localCode, string number, out PhoneNumber phoneNumber)
{
var valid = false;
phoneNumber = null;
try
{
ushort uCountryCode = 0;
ushort uLocalCode = 0;
int iNumber = 0;
// match only if all three numbers have been parsed successfully
valid = UInt16.TryParse(countryCode, out uCountryCode) &&
UInt16.TryParse(localCode, out uLocalCode) &&
Int32.TryParse(number, out iNumber);
if (valid)
phoneNumber = new PhoneNumber(uCountryCode, uLocalCode, iNumber);
}
catch (ArgumentException)
{
// still not match
}
return valid;
}
}

Related

C# performance - Regex vs. multiple Split [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working with a rather large set of strings I have to process as quickly as possible.
The format is quite fixed:
[name]/[type]:[list ([key] = [value],)]
or
[name]/[type]:[key]
I hope my representation is okay. What this means is that I have a word (in my case I call it Name), then a slash, followed by another word (I call it Type), then a colon, and it is either followed by a comma-separated list of key-value pairs (key = value), or a single key.
Name, Type cannot contain any whitespaces, however the key and value fields can.
Currently I'm using Regex to parse this data, and a split:
var regex = #"(\w+)\/(\w+):(.*)";
var r = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
var m = r.Match(Id);
if (m.Success) {
Name = m.Groups[1].Value;
Type= m.Groups[2].Value;
foreach (var intern in m.Groups[3].Value.Split(','))
{
var split = intern.Trim().Split('=');
if (split.Length == 2)
Items.Add(split[0], split[1]);
else if (split.Length == 1)
Items.Add(split[0], split[0]);
}
}
Now I know this is not the most optional case, but I'm not sure which would be the fastest:
Split the string first by the : then by / for the first element, and , for the second, then process the latter list and split again by =
Use the current mixture as it is
Use a completely regex-based
Of course I'm open to suggestions, my main goal is to achieve the fastest processing of this single string.
Its always fun to implement a custom parser. Obviously concerning code maintenance, Regex is probably the best choice, but if performance is an ultimate concern then you probably need a tailor made parser which even in the simplest syntaxes is quite a lot more of work.
I've whipped one up really quick (it might be a little hackish in some places) to see what it would take to implement one with some basic error recovery and information. This isn't tested in any way but I'd be curious, if its minimally functional, to know how well it stacks up with the Regex solution in terms of performance.
public class ParserOutput
{
public string Name { get; }
public string Type { get; }
public IEnumerable<Tuple<string, string>> KeyValuePairs { get; }
public bool ContainsKeyValuePairs { get; }
public bool HasErrors { get; }
public IEnumerable<string> ErrorDescriptions { get; }
public ParserOutput(string name, string type, IEnumerable<Tuple<string, string>> keyValuePairs, IEnumerable<string> errorDescriptions)
{
Name = name;
Type = type;
KeyValuePairs = keyValuePairs;
ContainsKeyValuePairs = keyValuePairs.FirstOrDefault()?.Item2?.Length > 0;
ErrorDescriptions = errorDescriptions;
HasErrors = errorDescriptions.Any();
}
}
public class CustomParser
{
private const char forwardSlash = '/';
private const char colon = ':';
private const char space = ' ';
private const char equals = '=';
private const char comma = ',';
StringBuilder buffer = new StringBuilder();
public ParserOutput Parse(string input)
{
var diagnosticsBag = new Queue<string>();
using (var enumerator = input.GetEnumerator())
{
var name = ParseToken(enumerator, forwardSlash, diagnosticsBag);
var type = ParseToken(enumerator, colon, diagnosticsBag);
var keyValuePairs = ParseListOrKey(enumerator, diagnosticsBag);
if (name.Length == 0)
{
diagnosticsBag.Enqueue("Input has incorrect format. Name could not be parsed.");
}
if (type.Length == 0)
{
diagnosticsBag.Enqueue("Input has incorrect format. Type could not be parsed.");
}
if (!keyValuePairs.Any() ||
input.Last() == comma /*trailing comma is error?*/)
{
diagnosticsBag.Enqueue("Input has incorrect format. Key / Value pairs could not be parsed.");
}
return new ParserOutput(name, type, keyValuePairs, diagnosticsBag);
}
}
private string ParseToken(IEnumerator<char> enumerator, char separator, Queue<string> diagnosticsBag)
{
buffer.Clear();
var allowWhitespaces = separator != forwardSlash && separator != colon;
while (enumerator.MoveNext())
{
if (enumerator.Current == space && !allowWhitespaces)
{
diagnosticsBag.Enqueue($"Input has incorrect format. {(separator == forwardSlash ? "Name" : "Type")} cannot contain whitespaces.");
}
else if (enumerator.Current != separator)
{
buffer.Append(enumerator.Current);
}
else
return buffer.ToString();
}
return buffer.ToString();
}
private IEnumerable<Tuple<string, string>> ParseListOrKey(IEnumerator<char> enumerator, Queue<string> diagnosticsBag)
{
buffer.Clear();
var isList = false;
while (true)
{
var key = ParseToken(enumerator, equals, diagnosticsBag);
var value = ParseToken(enumerator, comma, diagnosticsBag);
if (key.Length == 0)
break;
yield return new Tuple<string, string>(key, value);
if (!isList && value.Length != 0)
{
isList = true;
}
else if (isList && value.Length == 0)
{
diagnosticsBag.Enqueue($"Input has incorrect format: malformed [key / value] list.");
}
}
}
}

How to validate decimal places

Please let me know good way to validate decimal value, if decimal(4,2) it should accept 2 numeric and 2 decimal places.
var value = "44.29";
var dec = value.Split('.');
Then finding the length will can be used, I need a better culture specific way. I need a generic solution which can be applied to all decimal field.
Like:
validate(int before,int afterdecimal);
var valid = validate(2,2);
Need a generic cleaner solution for this
private static bool IsDecimal(string value, int before, int after)
{
if (value.Contains("."))
{
var parts = value.Split('.');
if (parts[0].Length == before && parts[1].Length == after)
return true;
}
else if(value.Length == before)
return false;
return true;
}
You can try like this:
[RegularExpression(#"^\d{1,2}(\.\d{0,2})$",ErrorMessage = "Value contains more than 2 decimal places")]
public decimal Value { get; set; }
If you whant just validate, try to use the mod:
44.29 % 1 = 0.29
From the above answers I was able to do it like this
string value = "2009.99";
if (IsDecimal(value, 4, 4))
{
Console.WriteLine("Valid");
}
private static bool IsDecimal(string value, int before, int after)
{
var r = new Regex(#"^\d{1," + before + #"}(\.\d{0," + after + #"})$");
return r.IsMatch(value);
}

How to validate a (country specific) phone number

A valid phone number contains:
Less than 9 characters
A "+" at the start
Only digits.
I'm trying to use regular expressions but I've only started using them and I'm not good at it. The code I have so far is:
static void Main(string[] args)
{
Console.WriteLine("Enter a phone number.");
string telNo = Console.ReadLine();
if (Regex.Match(telNo, #"^(\+[0-9])$").Success)
Console.WriteLine("correctly entered");
else
Console.WriteLine("incorrectly entered");
Console.ReadLine();
}
But I don't know how to check the length of the string this way. Any help is appreciated.
Jacek's regex works fine
public class Program
{
public static void Main()
{
Console.WriteLine("Enter a phone number.");
string telNo = Console.ReadLine();
Console.WriteLine("{0}correctly entered", IsPhoneNumber(telNo) ? "" : "in");
Console.ReadLine();
}
public static bool IsPhoneNumber(string number)
{
return Regex.Match(number, #"^(\+[0-9]{9})$").Success;
}
}
Your regex should look like this, you need information about char counter
#"^(\+[0-9]{9})$"
DON'T USE A REGULAR EXPRESSION!!
There are too many variables for a regex to be of any use. Instead, just remove all characters from your string that are not 0-9, and then check to see if you have the correct number of digits left. Then it doesn't matter what extra stuff the user includes or doesn't include... ()x-+[] etc etc, as it just strips them all out and only counts the characters 0-9.
I've got a string extension that works great, and allows for a wide range of formats. It accepts an IsRequired parameter. So, you can validate a phone number like this:
string phone = "(999)999-9999"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true
string phone ="1234567890"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true
string phone = ""
bool isValidPhone = phone.ValidatePhoneNumber(false) // not required, so returns true
string phone = ""
bool isValidPhone = phone.ValidatePhoneNumber(true) // required, so returns false
string phone ="12345"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false
string phone ="foobar"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false
Here's the code (assumes a 10-digit American phone number. Adjust accordingly):
public static class StringExtensions
{
/// <summary>
/// Checks to be sure a phone number contains 10 digits as per American phone numbers.
/// If 'IsRequired' is true, then an empty string will return False.
/// If 'IsRequired' is false, then an empty string will return True.
/// </summary>
/// <param name="phone"></param>
/// <param name="IsRequired"></param>
/// <returns></returns>
public static bool ValidatePhoneNumber(this string phone, bool IsRequired)
{
if (string.IsNullOrEmpty(phone) & !IsRequired)
return true;
if (string.IsNullOrEmpty(phone) & IsRequired)
return false;
var cleaned = phone.RemoveNonNumeric();
if (IsRequired)
{
if (cleaned.Length == 10)
return true;
else
return false;
}
else
{
if (cleaned.Length == 0)
return true;
else if (cleaned.Length > 0 & cleaned.Length < 10)
return false;
else if (cleaned.Length == 10)
return true;
else
return false; // should never get here
}
}
/// <summary>
/// Removes all non numeric characters from a string
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public static string RemoveNonNumeric(this string phone)
{
return Regex.Replace(phone, #"[^0-9]+", "");
}
}
Expanding upon one of the answers provided above, the method I came up with to also handle a few phone number delivery styles as well as international phone number is
internal static bool IsValidPhoneNumber(this string This)
{
var phoneNumber = This.Trim()
.Replace(" ", "")
.Replace("-", "")
.Replace("(", "")
.Replace(")", "");
return Regex.Match(phoneNumber, #"^\+\d{5,15}$").Success;
}
Something like this could work:
^+\d{0,9}
But I would suggest playing around with a regex tester to learn more about how regular expressions work. I still like to use them heavily myself, as I don't write regular expressions often. Here is one example but there are many more out there.
https://regex101.com/
Simple function for Valid USAPhoneNumber or not.
/// <summary>
/// Allows phone number of the format: NPA = [2-9][0-8][0-9] Nxx = [2-9] [0-9][0-9] Station = [0-9][0-9][0-9][0-9]
/// </summary>
/// <param name="strPhone"></param>
/// <returns></returns>
public static bool IsValidUSPhoneNumber(string strPhone)
{
string regExPattern = #"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$";
return MatchStringFromRegex(strPhone, regExPattern);
}
// Function which is used in IsValidUSPhoneNumber function
public static bool MatchStringFromRegex(string str, string regexstr)
{
str = str.Trim();
System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex(regexstr);
return pattern.IsMatch(str);
}
If you're looking for a country specific regex, try this expression which works for all Australian (+61-) numbers. I've put comments on how to go about varying it for other uses.
public static bool IsValidPhoneNumber(string phoneNumber)
{
//will match +61 or +61- or 0 or nothing followed by a nine digit number
return Regex.Match(phoneNumber,
#"^([\+]?61[-]?|[0])?[1-9][0-9]{8}$").Success;
//to vary this, replace 61 with an international code of your choice
//or remove [\+]?61[-]? if international code isn't needed
//{8} is the number of digits in the actual phone number less one
}
This solution validates every test criteria for validating a phone number, it also leverages from the Regex API. Criteria includes spacing, any non numeric values, area codes (which you specify), number of values (digits) the phone number should have, and also includes error messaging as well as phone number old and new state.
Here is the source code:
public class PhoneNumberValidator
{
public string ErrorMessage { get; set; }
public int PhoneNumberDigits { get; set; }
public string CachedPhoneNumber { get; set; }
private Dictionary<int, string> VaildAreaCodes()
{
return new Dictionary<int, string>
{
[3] = "0",
[4] = "27"
};
}
private bool IsInteger(string value)
{
return int.TryParse(value, out int result);
}
private string GetConsecutiveCharsInPhoneNumberStr(string phoneNumber)
{
switch (PhoneNumberDigits)
{
case 0:
case 10:
PhoneNumberDigits = 10;
return phoneNumber.Substring(phoneNumber.Length - 7);
case 11:
return phoneNumber.Substring(phoneNumber.Length - 8);
default:
return string.Empty;
}
}
private bool IsValidAreaCode(ref string phoneNumber, string areaCode)
{
if (!IsInteger(areaCode))
{
ErrorMessage = "Area code characters of Phone Number value should only contain integers.";
return false;
}
var areaCodeLength = areaCode.Length;
var invalidAreaCodeMessage = "Phone Number value contains invalid area code.";
switch (areaCodeLength)
{
case 2:
phoneNumber = string.Concat("0", phoneNumber);
return true;
case 3:
if (!areaCode.StartsWith(VaildAreaCodes[3]))
ErrorMessage = invalidAreaCodeMessage;
return string.IsNullOrWhiteSpace(ErrorMessage) ? true : false;
case 4:
if (areaCode.StartsWith(VaildAreaCodes[4]))
{
phoneNumber = string.Concat("0", phoneNumber.Remove(0, 2)); // replace first two charaters with zero
return true;
}
ErrorMessage = invalidAreaCodeMessage;
return false;
default:
ErrorMessage = invalidAreaCodeMessage;
return false;
}
}
public bool IsValidPhoneNumber(ref string phoneNumber)
{
CachedPhoneNumber = phoneNumber;
if (string.IsNullOrWhiteSpace(phoneNumber))
{
ErrorMessage = "Phone Number value should not be equivalent to null.";
return false;
}
phoneNumber = Regex.Replace(phoneNumber, " {2,}", string.Empty); // remove all whitespaces
phoneNumber = Regex.Replace(phoneNumber, "[^0-9]", string.Empty); // remove all non numeric characters
var lastConsecutiveCharsInPhoneNumberStr = GetConsecutiveCharsInPhoneNumberStr(phoneNumber);
if (string.IsNullOrWhiteSpace(lastConsecutiveCharsInPhoneNumberStr))
{
ErrorMessage = "Phone Number value not supported.";
return false;
}
if (!IsInteger(lastConsecutiveCharsInPhoneNumberStr))
{
ErrorMessage = "Last consecutive characters of Phone Number value should only contain integers.";
return false;
}
var phoneNumberAreaCode = phoneNumber.Replace(lastConsecutiveCharsInPhoneNumberStr, "");
if (!IsValidAreaCode(ref phoneNumber, phoneNumberAreaCode))
{
return false;
}
if (phoneNumber.Length != PhoneNumberDigits)
{
ErrorMessage = string.Format("Phone Number value should contain {0} characters instead of {1} characters.", PhoneNumberDigits, phoneNumber.Length);
return false;
}
return true;
}
}
The solution is highly configurable and may be used for any digits phone number as well as area code.

Test Console.ReadLine(); input type

I'm making a console app in which I'll ask the user for a phone number int main_phone, but if he types a string thats not convertible to int such as "Caramel" I want to give him a warning and ask for a number.
Well I thought a recursive function would do, but I'm lacking logical aptitude for this one. What I came up with was the following code snippet:
static int? TryParse(string str)
{
int tmp;
if (int.TryParse(str, out tmp)) {
return tmp;
}
return null;
}//End of TryParse();
static int? verifyParse(string str, string lable)
{
int? testedVar = TryParse(str);
if (testedVar == null)
{
Console.WriteLine("Este campo pode apenas conter dígitos de 0 à 9.");
Console.Write(lable);
verifyParse(Console.ReadLine(), lable);
return null;
}
else
{
return testedVar;
}
}//End of verifyParse();
Console.Write("Phone(main):");
int? main_phone = verifyParse(Console.ReadLine(),"Telefone (Principal):");
The problem is that if the user inputs "string" main_phone will comeback as null, then it asks for a number between 0 and 9, but even if the user passes a convertible string , such as "12", main_phone will still be null.
How can I fix that?
EDIT 1:
What I really need is a function that if the string can be converted to an int it returns the int, else it gives me an error and asks me to input something between 0 and 9. I find this hard because the function would have to be able to return an int or run it self all over again(to require and test a valid input), but it can't because either it returns the int or it doesn't return nothing.
EDIT 2:
Here is a little update on my problem:
static int TryParse(string lable)
{
string str = Console.ReadLine();
Regex regex = new Regex(#"\d+");
while (!regex.IsMatch(str))
{
Console.WriteLine("Insira apenas dígitos de 0-9, por favor.");
Console.Write(lable);
str = Console.ReadLine();
}
return Convert.ToInt32(str);
}//End of TryParse();
Console.Write("Telefone (Principal):");
int main_phone = TryParse("Telefone (Principal):");
It works really well, except when I input something like "1a". In that case I'll get this error:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at App1.Program.TryParse(String lable)
at App1.Program.Main(String[] args)
Here's a solution that loops until a good match is produced, using regex validation
Regex regex = new Regex(#"\d{3}-?\d{3}-?\d{4}");
string phonenumber = Console.ReadLine();
while (!regex.IsMatch(phonenumber))
{
Console.WriteLine("Bad Input");
phonenumber = Console.ReadLine();
}
return Convert.ToInt32(phonenumber.Replace("-",""));
which will match 5555555555 and 555-555-5555, and return the int value.
How about you just read in as a string what the user puts in, replace all "-" with "", then check if it numeric and check length. No need to make a recursive method for such a simple task.
how about this
[VB.NET]
Function CheckPhoneNumber(ByVal Number As String) As String
Number = Number.Replace("-","")
Number = Number.Replace("(","")
Number = Number.Replace(")","")
If Not IsNumeric(Number) Then
Return "Error"
Else
Return Number
End If
End Function
[C#]
public string CheckPhoneNumber(string Number)
{
Number = Number.Replace("-", "");
Number = Number.Replace("(", "");
Number = Number.Replace(")", "");
if (!Information.IsNumeric(Number)) {
return "Error";
} else {
return Number;
}
}
What about just using int.TryParse?
void Main()
{
var input = Console.ReadLine();
int phone;
while(!int.TryParse(input, out phone)){
Console.WriteLine("Please enter a number");
input = Console.ReadLine();
}
Console.WriteLine("Success");
}
To cleanup the input, you can replace the "-"
var input = Console.ReadLine().Replace("-", "");
Could strip out any extra characters ('+', '-', '#', '(', ')', and '*') then try
bool successful;
int result;
if(!int.tryparse(string, result))
{
Console.WriteLine("Invalid phone number");
}
else
{
Console.WriteLine("Valid phone number");
}
Or whatever you like.
Of course the alternative is to use REGEX. (note: I'm stealing this from Regex Hero found here: http://regexhero.net/tester/)
string strRegex = #"[+]?[\d][\d\s()-.]{8,}[\d]";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
Try this modified version of your code segment:
static int? verifyParse(string str, string lable)
{
int? testedVar = TryParse(str);
if (testedVar == null)
{
Console.WriteLine("Este campo pode apenas conter dígitos de 0 à 9.");
Console.Write(lable);
int? newVal = verifyParse(Console.ReadLine(), lable);
if (newVal != null)
return newVal;
else
return null;
}
else
{
return testedVar;
}
}//End of verifyParse();

Need to return the value with an Currency sign

I have a small tab on an app that allows user to lookup a price in a price table and it returns the price. how can i get the '$' sign on the returned price?
public double? SearchMedicinePrice(Int64 pNDC)
{
double? retValue = null;
objConext = new FDBEntities();
Medicine objMedicine = objConext.Medicines.Where(med => med.PriceType == 9 && med.NDC == pNDC).OrderByDescending(item=>item.MedicineID).FirstOrDefault();
if (objMedicine != null)
{
retValue = objMedicine.Price;
}
return retValue;
}
When you call ToString() on a double (and many other types), you can pass in a format string to specify how the result should be formatted. Like this:
double price = 10.5;
price.ToString("C");
Output = $10.50
You could also use String.Format like this:
string message = String.Format("The price is {0:C}", SearchMedicinePrice(pndc));
{0:C} is a format string. The C specifies that the value should be formatted as currency.
You can also pass in a CultureInfo object to configure things like the currency symbol. For example, the following code will output £10.50
10.5.ToString("C", new CultureInfo("en-GB"))
You might also consider using decimal instead of double for storing currency data.
You could build a small object:
public class Currency {
public double Amount {get;set;}
public string Symbol {get;set;}
public override string ToString() {
return String.Format(Symbol + "{0}",Amount);
}
}
public Currency SearchMedicinePrice(Int64 pNDC) {
Currency retValue = null;
objConext = new FDBEntities();
Medicine objMedicine = objConext.Medicines.Where(med => med.PriceType == 9 && med.NDC == pNDC).OrderByDescending(item=>item.MedicineID).FirstOrDefault();
if (objMedicine != null)
{
retValue.Amount = objMedicine.Price;
retValue.Symbol = objMedicine.CurrencySymbol;
}
return retValue;
}
Then in to use it:
Label1.Text = SearchMedicinePrice(pNDC).ToString();
You have to use string
string str="$"+SearchMedicinePrice(x).ToString();

Categories