Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Validating User Input | Validating Input and Safe Parsing
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Exceptions and Error Handling Practice

bookValidating User Input

Program.cs

Program.cs

copy
123456789101112131415161718192021222324
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { Console.Write("Enter your age: "); string input = Console.ReadLine(); int age; if (!int.TryParse(input, out age) || age <= 0) { Console.WriteLine("Invalid input. Age must be a positive integer."); } else { Console.WriteLine($"Your age is: {age}"); } } } }
ValidationExamples.cs

ValidationExamples.cs

copy
123456789101112131415161718192021222324
// This file is for illustration only and is not meant to be run directly. using System.Text.RegularExpressions; public class ValidationExamples { // Email format validation public bool IsValidEmail(string email) { // Simple pattern for demonstration; real validation may require more checks return Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"); } // Password strength validation public bool IsStrongPassword(string password) { // At least 8 characters, one uppercase, one lowercase, one digit return password.Length >= 8 && Regex.IsMatch(password, @"[A-Z]") && Regex.IsMatch(password, @"[a-z]") && Regex.IsMatch(password, @"\d"); } }
question mark

Select the correct answer

question mark

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how the regular expression for email validation works?

What are some common mistakes to avoid when validating passwords?

Can you show more examples of input validation in C#?

bookValidating User Input

Sveip for å vise menyen

Program.cs

Program.cs

copy
123456789101112131415161718192021222324
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { Console.Write("Enter your age: "); string input = Console.ReadLine(); int age; if (!int.TryParse(input, out age) || age <= 0) { Console.WriteLine("Invalid input. Age must be a positive integer."); } else { Console.WriteLine($"Your age is: {age}"); } } } }
ValidationExamples.cs

ValidationExamples.cs

copy
123456789101112131415161718192021222324
// This file is for illustration only and is not meant to be run directly. using System.Text.RegularExpressions; public class ValidationExamples { // Email format validation public bool IsValidEmail(string email) { // Simple pattern for demonstration; real validation may require more checks return Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"); } // Password strength validation public bool IsStrongPassword(string password) { // At least 8 characters, one uppercase, one lowercase, one digit return password.Length >= 8 && Regex.IsMatch(password, @"[A-Z]") && Regex.IsMatch(password, @"[a-z]") && Regex.IsMatch(password, @"\d"); } }
question mark

Select the correct answer

question mark

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1
some-alt