Credit Card validator for java

半世苍凉 提交于 2019-12-03 09:07:26

You can find custom implantation of credit card validator here which is doing both credit card number validation plus credit card type detection,

http://www.esupu.com/credit-card-validator-java/

I did this a long time ago, Sorry Code is in C. Easily Convertible. Hope this will help you.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int CardNoChecker(unsigned long long int Number)
{
        int dijts=0;
        int Ans=0;
        {
            unsigned long long int k=1;
            while(Number%k!=Number)
            {
                    dijts=dijts+1;
                    k=k*10;
            }
        }
    {
        int i=1;
        int Product=0;
        int Sum=0;
        for(i=dijts;i>=1;i--)
        {
                if(i%2==0)
                {
                    if((Number%10)*2<10)
                    Product = Product + (  Number % 10  ) * 2 ;
                    else
                    {
                        int No=(Number%10)*2;
                        Product = Product + No/10;
                        Product = Product + No%10;
                    }
                }
                else
                {
                    Sum = Sum + (  Number  % 10     ) ;
                }
                Number=Number /10;
        }
        Ans = Sum + Product ;
    }
    if(Ans%10==0)
    return (1);
    else
    return (0);
}

int main()
{
    unsigned long long int CardNO;
    int valid=0;
    while(!valid)
    {
        int CnV=0;
        int VC=0;
        int AE=0;
        int MC=0;
        printf("Enter Card NO : ");
        scanf("%llu",&CardNO);
        if(CardNO/1000000000000==4 || CardNO/1000000000000000==4)
        {
            VC=1;
        }
        else if(CardNO/10000000000000==34 ||CardNO/10000000000000==37)
        {
            AE=1;
        }
        else if(CardNO/100000000000000==51 || CardNO/100000000000000==52 || CardNO/100000000000000==53 || CardNO/100000000000000==54 || CardNO/100000000000000==55)
        {
            MC=1;
        }
        CnV=CardNoChecker(CardNO);
        if(CnV && MC )
         printf("This is a Valid Master Card\n\n");
         else if(CnV && VC )
         printf("This is a Valid Visa Card\n\n");
         else if(CnV && AE )
         printf("This is a Valid American Express Card\n\n");
         else
        printf("Card is Not Valid'\n\n");
    }

    return (0);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!