首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >信用卡验证前端

信用卡验证前端
EN

Stack Overflow用户
提问于 2019-03-05 04:20:59
回答 1查看 374关注 0票数 1

大家好,我是Xamarin.forms的初学者。我正在尝试将信用卡验证添加到我的代码。我收到一个错误,说对象验证没有设置为对象的一个实例。

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

代码语言:javascript
复制
    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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-05 04:36:45

如果用户没有输入任何内容,则CreditCard.Text将为空。您只是在测试空字符串

替换

代码语言:javascript
复制
if(creditcardnum!="")

使用

代码语言:javascript
复制
if (!string.IsNullOrEmpty(CreditCard.Text))

同时测试空字符串和null值

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54990950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档