A simple validation routine.
★☆☆Online forms will check that an input is valid before sending data to a server. For example, when entering an expiry date for a debit card, the month must be between 1 and 12 to be accepted. Doing the basic checks at the client side reduces traffic to and load on the server side.
Write a program that keeps asking the user to enter the month until a valid month has been entered. Valid months are 1-12.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
validate_month so that it:true if the month is greater than or equal to one and less than or equal to twelve.false in all other cases.Enter a month 1-12:
5
Thank you. Input accepted.
Enter a month 1-12:
-8
Enter a month 1-12:
0
Enter a month 1-12:
13
Enter a month 1-12:
99
Enter a month 1-12:
3
Thank you. Input accepted.
// Valid month program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
// Check the month is a valid number
---
static bool validate_month(int month)
{
---
// Must be between 1 and 12
if ((month > 0) && (month <= 12))
{
---
return true;
---
}
---
else
{
---
return false;
---
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
bool valid_month = false;
---
while (!valid_month)
{
---
Console.WriteLine("Enter a month 1-12:");
int month = Convert.ToInt32(Console.ReadLine());
valid_month = validate_month(month);
---
}
---
Console.WriteLine("Thank you. Input accepted.");
---
}
---
}