Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating my money

I am having some trouble validating a money input. I have used some pointers from my other questions to write some better code. The following is what I'm using to validate whether or not the input is money.

static void Main(string[] args)
{
    string myTest1 = "$1,234.56";
    string myTest2 = "$1.00";
    string myTest3 = "$1000.01";
    string myTest4 = "$1,234,567.89";

    myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator();

    bool myResult1 = Miimv.isMaybeMoney(myTest1);
    bool myResult2 = Miimv.isMaybeMoney(myTest2);
    bool myResult3 = Miimv.isMaybeMoney(myTest3);
    bool myResult4 = Miimv.isMaybeMoney(myTest4);
}

public bool isMaybeMoney(object theirMaybeMoney)
{
    string myMaybeMoney = theirMaybeMoney.ToString();

    if (myMaybeMoney.StartsWith("-"))
    {
        myMaybeMoney.Remove(0, 1);
    }

    if (!myMaybeMoney.StartsWith("$"))
    {
        return false;
    }

    myMaybeMoney.Remove(0, 1);

    string[] myMaybeMoneyStringArray = myMaybeMoney.Split('.');

    string myMaybeMoneyDollars = myMaybeMoneyStringArray[0];
    string myMaybeMoneyCents = myMaybeMoneyStringArray[1];

    if (!isDollars(myMaybeMoneyDollars))
    {
        return false;
    }

    if (!isCents(myMaybeMoneyCents))
    {
        return false;
    }

    return true;
}

private bool isDollars(string theirMaybeMoneyDollars)
{
    if (!isNumber(theirMaybeMoneyDollars))
        return false;

    try
    {
        int myMaybeDollars = Convert.ToInt32(theirMaybeMoneyDollars);

        if (myMaybeDollars < 1)
            return false;

        return true;
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}

private bool isCents(string theirMaybeMoneyCents)
{
    if (!isNumber(theirMaybeMoneyCents))
        return false;

    try
    {
        int myMaybeCents = Convert.ToInt32(theirMaybeMoneyCents);

        if (myMaybeCents > 99)
            return false;
        if (myMaybeCents < 1)
            return false;

        return true;
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}

private bool isNumber(object theirMaybeNumber)
{
    return Microsoft.VisualBasic.Information.IsNumeric(theirMaybeNumber);
}

The results are all not money, which is pretty confusing to me.

like image 941
Raging Dave Avatar asked Feb 07 '12 19:02

Raging Dave


People also ask

What does validate payment mean?

Designed to help Corporate and Financial Institutions easily validate digital bank payments, a validation API will automatically do a thorough data check to verify the account details of the payment recipients, essentially verifying that a payment can be processed through this channel before it's initiated.

What is pre validation?

The optimal model for a pre-validation is that after a pre-defined number of recorded speakers the speech signals, the annotations, the meta data and documentation files are transferred to an external validation center which will perform a formal validation of the data.


1 Answers

Looks like there is a builtin validation for money in C#.

float num;
bool isValid = float.TryParse(str, 
NumberStyles.Currency,
CultureInfo.GetCultureInfo("en-US"), // cached
out num);

FROM: https://stackoverflow.com/a/617847/290822

like image 195
Laurence Burke Avatar answered Oct 20 '22 00:10

Laurence Burke