# Validate a currency string

Please see the code below:

public bool IsValidateInputSuceeded(string EnteredCost, string EnteredPaymentReceived, out decimal Cost, out decimal PaymentReceived)
{
bool ValidateInputSuceeded = true;

EnteredCost= EnteredCost.Substring(EnteredCost.IndexOfAny("0123456789".ToCharArray()));

if ((Decimal.TryParse(EnteredCost.Replace(" ", "").Replace(",", ""), out Cost)) == false)
{
ValidateInputSuceeded = false;
}

if ((Decimal.TryParse(EnteredPaymentReceived.Replace(" ", "").Replace(",", ""), out PaymentReceived)) == false)
{
ValidateInputSuceeded = false;
}

if (ValidateInputSuceeded == false)
{
return false;
}

if (EnteredCost.IndexOf(" ") > 0|| EnteredPaymentReceived.IndexOf(" ") > 0)
{
return false;
}

if (EnteredCost[EnteredCost.Length-1] =='.' || EnteredCost[EnteredCost.Length - 2] == '.' || EnteredPaymentReceived[EnteredPaymentReceived.Length - 1]=='.' || EnteredPaymentReceived[EnteredPaymentReceived.Length - 2] == '.')
{
return false;
}

if (EnteredCost.IndexOf(".") == -1 || EnteredPaymentReceived.IndexOf(".")==-1)
{
return false;
}

return true;
}


EnteredCost and EnteredPaymentReceived are input parameters from MaskedTextBoxs'(https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox(v=vs.110).aspx) both with an input mask of: £999,999.00. For example, a value of one of them could be: "£ , 3.22" (without quotes) as an input parameter into the function.

There are two string variables: EnteredCost and PaymentReceived. Should these variables be StringBuilders instead of Strings? The reason I ask is because there are a log of string manipulations going on e.g. using the REPLACE function and the indexOfAny function, which returns more strings.

• Please take a moment to rewrite the title and write what your code is doing and not your concerns about it. If you are not sure how to do it see How do I ask a good question? also please explain the code a little bit more. – t3chb0t May 7 '17 at 10:32
• StringBuilder helps with concatenation, not replace or index of. Describing the desired validation method might help with suggesting a faster/cleaner approach. – D. Jurcau May 7 '17 at 10:38
• @D. Jurcau , I have edited the question with more detail. Does that help? – w0051977 May 7 '17 at 10:47
• @t3chb0t, I have edited the question. Does that help? – w0051977 May 7 '17 at 10:47
• Yep, I like it much better now however the title still needs to bo improved. It's validating a payment string so maybe try to write something about it like validating payment string has some format etc. You know better what the method does. The title should reflect this. – t3chb0t May 7 '17 at 10:50

Since

EnteredCost and EnteredPaymentReceived are input parameters from MaskedTextBox

and a MaskedTextBox

Uses a mask to distinguish between proper and improper user input.

then the validation is not necessary because all it does is to check whether the strings contain numeric data and a dot . as a separator. The MaskedTextBoxs alredy do that so why are you validating their input again?

• The text can appear like this: " , 9.52", which cannot be casted into a decimal or like this: "£ , 9,5 " (space on the end) or like this: "£ , 9. 2" (second to last character is a space). Therefore I believe validation is necessary? – w0051977 May 7 '17 at 11:58
• @w0051977 you could set the TextMaskFormat = MaskFormat.ExcludePromptAndLiterals so the Text property will give you a clean int, you can then easily parse it and multiply with 0.01 and the the actuall value (assuming the mask is something like £ 0.00. – t3chb0t May 7 '17 at 12:08
• @w0051977 The text can appear like this: " , 9.52", [..] or like this: "£ , 9,5 [..] or like this: "£ , 9. 2" how so? If it's a masked-text-box then why are so many formats allowed? It doesn't make sense. Didn't you set the Mask property? If you didn't then this control hasn't any advantage over an ordinary text-box and this would explain why the manual validation. – t3chb0t May 7 '17 at 12:16
• the mask is: £999,999.00. This means someone can enter: £ , 9. 2, doesn't it? – w0051977 May 7 '17 at 12:31
• @w0051977 when I use the Mask = "£999,999.00" it's not possible to enter anything else but a digit or a space so I don't understand where the 50p should come from. The user shouldn't even be able to use the P or any other letter. It's also not possible to type any separators either. – t3chb0t May 7 '17 at 13:05