需要验证文本框的值

时间:2016-08-16 15:22:16

标签: c#

我有一个文本框,需要在点击“添加”按钮时进行验证。

当其他两个检查不在代码中时,第一次检查正常。

如果我在文本框中输入一个值,那么最后两个检查就可以了。但是当我有三个检查时,如果没有值,程序会抛出一个错误说

  

“输入字符串的格式不正确”。

class BookingsController < ApplicationController

    before_action :authenticate_user!

    def new
        # booking form
        # I need to find the event that we're making a booking on
        @event = Event.find(params[:event_id])
        # and because the event "has_many :bookings"
        @booking = @event.bookings.new(quantity: params[:quantity])
        # which person is booking the event?
        @booking.user = current_user
        #@booking.quantity = @booking.quantity
        #@total_amount = @booking_quantity.to_f * @event_price.to_f

    end

    def create
        # actually process the booking
        @event = Event.find(params[:event_id])
        @booking = @event.bookings.new(booking_params)
        @booking.user = current_user
        #@total_amount = @booking.quantity.to_f * @event.price.to_f

        Booking.transaction do

            @event.reload
            if @event.bookings.count > @event.number_of_spaces
            flash[:warning] = "Sorry, this event is fully booked."
            raise ActiveRecord::Rollback, "event is fully booked"
            end 
        end

        if @booking.save

            # CHARGE THE USER WHO'S BOOKED
            # #{} == puts a variable into a string
            Stripe::Charge.create(amount: @event.price_pennies, currency: "gbp",
                card: @booking.stripe_token, description: "Booking number #{@booking.id}")

            flash[:success] = "Your place on our event has been booked"
            redirect_to event_path(@event)
        else
            flash[:error] = "Payment unsuccessful"
            render "new"
        end

        if @event.is_free?

            @booking.save!
            flash[:success] = "Your place on our event has been booked"
            redirect_to event_path(@event)
        end
    end

    #def total_amount
        #@total_amount = @booking.quantity * @event.price
    #end

    private

    def booking_params
        params.require(:booking).permit(:stripe_token, :quantity)
    end

end

2 个答案:

答案 0 :(得分:5)

如果您确定输入字符串为空或有效数字,则可以使用:

        if (txtAge.Text == "")
        {
            message += "<br>Please fill in your Age.</br>";
        }
        else{
            if (Convert.ToInt32(txtAge.Text) > 120)
            {
                message += "<br>Age cannot be greater than 120</br>";
            }
            if (Convert.ToInt32(txtAge.Text) < 6)
            {
                message += "<br>Age cannot be less than 6</br>";
            }
        }

否则你也应该使用int.TryParse方法来确保字符串是有效数字。

答案 1 :(得分:2)

无法将空字符串转换为Int32。您应该使用int.TryParse代替(因此您甚至不需要解析该值两次):

int age;
if (!int.TryParse(txtAge.Text, out age))
    message += "<br>Age is not a number!</br>";
else if (age > 120)
{
    message += "<br>Age cannot be greater than 120</br>";
}
else if (age < 6)
{
    message += "<br>Age cannot be less than 6</br>";
}

并使用else if因为如果它根本不是数字,那么检查它是否在特定范围内是没有意义的。