-2

I'm 100% new to programming, this is what I want:

  • (Name) No numbers
  • (Card Number) Limit to 16 digits and no letters
  • (Expiry Date) Numbers like this - "02/17" and no letters
  • (Security Code) Limit to 3 numbers and no letters.

My code:

string message =
    "Name: " + nameTextBox.Text +
    "\nCard Number: " + cardNumberTextBox.Text +
    "\nExpiry Date: " + expiryDateTextBox.Text +
    "\nSecurity Code: " + securityCodeTextBox.Text +
    "\nOrder: Pizza " + pizzaType + ", " + pizzaSize;

if (TotalToppingQuantities() > 0)
{
    for (int toppingIndex = 0; toppingIndex < toppingQuantities.Length; toppingIndex++)
    {
        if (toppingQuantities[toppingIndex] > 0)
        {
            message += ", " + toppingQuantities[toppingIndex] + " x " +
                       toppingNames[toppingIndex];
        }
    }
}

message +=
    "\nPickup Spot: " + pickupSpot +
    "\nDelivery Time: 30 minutes";

MessageBox.Show(message);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

2

For your problem, regex is good solution.

And this should work for you

using System.Text.RegularExpressions;
//====================================


if (Regex.Match(nameTextBox.Text, "\\d").Success) 
{
    MessageBox.Show("(Name) must contain No numbers");
    return ;
}
if (!Regex.Match(cardNumberTextBox.Text, "^\\d{16}$").Success) 
{
    MessageBox.Show("(Card Number) must be Limited to 16 digits and no letters");
    return ;
}
if (!Regex.Match(expiryDateTextBox.Text, "^\\d{2}/\\d{2}$").Success) 
{
    MessageBox.Show("(Expiry Date) must be Numbers like this - 02/17 and no letters");
    return ;
}
if (!Regex.Match(securityCodeTextBox.Text, "^\\d{3}$").Success) 
{
    MessageBox.Show("(Security Code) must be Limited to 3 numbers and no letters.");
    return ;
}
jpaugh78
  • 99
  • 2
  • 10
Pham X. Bach
  • 5,284
  • 4
  • 28
  • 42
  • How would I go about inputting that into my code? This is my program: https://drive.google.com/folderview?id=0B5tyNtwtsowgZTlTbUhNaE90WWM&usp=drive_web – Connor O'Keeffe May 24 '16 at 12:47
  • @ConnorO'Keeffe ah, got it. For validation event - fire right after user typing, you should go to [c-sharp-validating-input-for-textbox-on-winforms](http://stackoverflow.com/questions/8915151/c-sharp-validating-input-for-textbox-on-winforms) as suggested by `Dickson Xavier`. Just copy each `if-statement` of code in my answer in the right place for each validation event. – Pham X. Bach May 24 '16 at 13:13
  • Sorry, I honestly don't know where to input the if statements. Whenever I copy if (Regex.Match(nameTextBox.Text, "\\d").Success) { it just gives me errors – Connor O'Keeffe May 24 '16 at 13:39
  • @ConnorO'Keeffe What is the errors message? Did you import library `using System.Text.RegularExpressions;`? – Pham X. Bach May 24 '16 at 14:36