信用卡验证前端

时间:2019-03-04 20:20:59

标签: c# xamarin.forms xamarin.android xamarin.ios

大家好,            我是Xamarin.forms的初学者。我正在尝试将信用卡验证添加到我的代码中。我收到一条错误消息,指出对象验证未设置为对象的实例。

我附上我的代码和示例图片,以供进一步参考。  我希望信用卡号的长度为16。日期格式应为MM / YY,而CVV应该是数字。如果不满足这些条件,我想在表单字段下方显示错误文本。

    int SchoolId, UserId, EventId;

    public CardDetailsPage(int schoolId, int userId, int eventId)
    {
        SchoolId = schoolId;
        EventId = eventId;
        UserId = userId;
        InitializeComponent();
    }

    private async void Handle_Clicked(object sender, System.EventArgs e)
    {
        var creditcardnum = CreditCard.Text;
        var CVV = cvc.Text;
        var date = monthyear.Text;

          if(creditcardnum!="")
        {
            if(creditcardnum.Length < 16)
            CardError.Text = "Please Enter a Valid Credit Card Number";
        }
        else
        {
            CardError.Text = "Credit Card Number Cannot be null";
        }

        if (CVV != null && CVV.Length > 4 || CVV.Length < 3)
        {
            CVVError.Text = "Please Enter a Valid Credit CVV";
        }
        else
        {
            CVVError.Text = "Plese Enter a CVV";
        }

        var monthYear = new Regex(@"^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$");

        if (date!= "" && !monthYear.IsMatch(date)) // <2>check cvv is valid as "999"
            DateError.Text = "Please Check Your Month/Year Format";
        else
        {
            DateError.Text = "Enter the Expiration Date";
        }

        if (string.IsNullOrWhiteSpace(CVVError.Text) || string.IsNullOrWhiteSpace(CardError.Text) || string.IsNullOrWhiteSpace(DateError.Text))
        {
            /// Call api to complete purchase
            BuyEventPlayload buyPlayload = new BuyEventPlayload()
            {
                EventId = EventId,
                SchoolId = SchoolId,
                UserId = UserId,
                CardNumber = creditcardnum,
                CVV = CVV,
                ExpDate = date,
                FullName = "Test User"
            };
            HttpContent content = new StringContent(JsonConvert.SerializeObject(buyPlayload), Encoding.UTF8, "application/json");

            var appResponse = await CommonUtility.PostAsyncContent(App.CurrentConfiguration.BuyEventUrl, content);

            if (appResponse.IsSuccessStatusCode)
            {
                string eventsJson = await appResponse.Content.ReadAsStringAsync();

                EventList objeventlist = new EventList();
                objeventlist = JsonConvert.DeserializeObject<EventList>(eventsJson);

                foreach (Event ent in objeventlist.Events)
                {
                    if (ent.Id == EventId)
                    {
                        MessagingCenter.Send<CardDetailsPage, Event>(this, "", ent);
                        break;
                    }
                }
                await PopupNavigation.Instance.PopAsync(true);

            }
        }

    }
}

}

CardDetailsPage.Xaml

<?xml version="1.0" encoding="UTF-8"?>
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                 x:Class="Project.Views.[enter image description here][1]CardDetailsPage" 
                 xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup" 
                 xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup">
    <pages:PopupPage.Animation>
        <animations:ScaleAnimation DurationIn="400" DurationOut="300" ScaleOut="0.8" ScaleIn="1.2" EasingIn="SinOut" EasingOut="SinIn" PositionIn="Center" PositionOut="Center" HasBackgroundAnimation="true" />
    </pages:PopupPage.Animation>
    <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="Center" Margin="12" Padding="24" Spacing="24" BackgroundColor="White">
        <StackLayout Orientation="Horizontal">
            <Image Source="CreditCard.png" />
            <Entry x:Name="CreditCard" Placeholder="Enter Card Number" WidthRequest="250" HeightRequest="50" MaxLength="16" Keyboard="Numeric" />
        </StackLayout>
        <Label x:Name="CardError" TextColor="Red" FontSize="12" FontFamily="Sans-Serif"/>
        <StackLayout Orientation="Horizontal">
            <StackLayout Orientation="Horizontal">
                <Image Source="MonthDate.png" />
                <Entry x:Name="monthyear" Placeholder="MM/YY" Text="{Binding Date, StringFormat=MM-yy}" WidthRequest="100" HeightRequest="50" />
            </StackLayout>
            <Label x:Name="DateError" TextColor="Red" FontSize="12" FontFamily="Sans-Serif"/>
            <StackLayout Orientation="Horizontal">
            <Image Source="cvc.png" />
            <Entry x:Name="cvc" Placeholder="CVC" WidthRequest="100" HeightRequest="50" Keyboard="Numeric" MaxLength="4"/>
            </StackLayout>  
             <Label x:Name="CVVError" TextColor="Red" FontSize="12" FontFamily="Sans-Serif"/>
        </StackLayout>
        <Button HorizontalOptions="Center" Text="PayNow" VerticalOptions="Center" TextColor="White" BackgroundColor="#00ccff" WidthRequest="250" FontSize="17" FontFamily="Sans-Serif" Clicked="Handle_Clicked" />
        <Label x:Name="ErrorText" TextColor="Red" BackgroundColor="Transparent" WidthRequest="250" XAlign="Center" YAlign="Start" VerticalOptions="StartAndExpand" FontSize="8"/>
    </StackLayout>
</pages:PopupPage>

1 个答案:

答案 0 :(得分:2)

如果用户未输入任何内容,则CreditCard.Text将为空。您只测试一个空字符串

替换

if(creditcardnum!="")

使用

if (!string.IsNullOrEmpty(CreditCard.Text))

同时测试一个空字符串和一个空值