信用卡验证前端

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (38)

大家好,我是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>
提问于
用户回答回答于

如果用户没有输入任何内容,那么CreditCard.Text将为null。您只测试一个空字符串

更换

if(creditcardnum!="")

如下

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

测试空字符串和空值

所属标签

可能回答问题的人

  • HKC

    红客学院 · 创始人 (已认证)

    27 粉丝7 提问5 回答
  • 西风

    renzha.net · 站长 (已认证)

    9 粉丝1 提问4 回答
  • 螃蟹居

    1 粉丝0 提问2 回答
  • 富有想象力的人

    2 粉丝0 提问1 回答

扫码关注云+社区

领取腾讯云代金券