user3396362
user3396362

Reputation:

Letter-only validation for user input in C# with WPF

I'm fairly new to using C# and I've been working on a small game to help me get used to the language. I want players to be able to input a letters-only name to be used throughout the course of the game; however, I've run into several issues trying to get the validation to work.

Here is the XAML code:

<Window x:Class="COMP4_Project.InputWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="New Game" Height="120" Width="250" ResizeMode="CanMinimize">
<Grid Margin="10">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />

    </Grid.RowDefinitions>
    <Label>Enter name:</Label>
    <TextBox x:Name="userInput" Grid.Column="2" Margin="0,0,0,0"></TextBox>
    <Button x:Name="Confirm" Click="Confirm_Click" Grid.Row="2" Width="80" Height="25" Content="Confirm" Margin="0,10,0,41" />
    <Button x:Name="Cancel" Click="Cancel_Click" Grid.Row="2" Grid.Column="1" Width="80" Height="25" Content="Cancel" HorizontalAlignment="Right" Margin="54,10,0,41" />
</Grid>

And here is my codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace COMP4_Project
{
/// <summary>
/// Interaction logic for InputWindow.xaml
/// </summary>
public partial class InputWindow : Window
{
    public InputWindow()
    {
        InitializeComponent();
    }

    private void Confirm_Click(object sender, RoutedEventArgs e)
    {
        string playerName;
        string userInput = "";

        Regex r = new Regex("^[a-zA-Z]*$");
        if (r.IsMatch(userInput))
        {
            MessageBox.Show("onlyletter");
            playerName = userInput;
            Window win = new GameWindow();
            win.Owner = this;
            win.ShowDialog();
        } 
        else
        {
            MessageBox.Show("Only letters permitted. Try again!");
        }
    }

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        Close();

    }
}
}

I've tried several methods of getting the validation to work; this is as close as I've gotten so far without breaking the application. It seems to accept any input despite the use of validation, including input with numbers and input where nothing has been typed at all (I tried changing the * in regex to a + to solve this, but when I did it wouldn't accept any input).

I feel like this issue has something to do with how I declared the variable for userInput, but I'm not sure how to work around it without more errors popping up.

Upvotes: 0

Views: 2194

Answers (2)

Oscar Bralo
Oscar Bralo

Reputation: 1907

You are doing this:

string userInput = "";

instead of:

string user = userInput.Text;

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

You're defining userInput as an empty string inside the Click event.

private void Confirm_Click(object sender, RoutedEventArgs e)
{
    string playerName;
    string userInput = "";  // An empty string is a match for your pattern

    Regex r = new Regex("^[a-zA-Z]*$");
    if (r.IsMatch(userInput))
    {
       ...

Set userInput to the value in the TextBox where they're entering their name.

private void Confirm_Click(object sender, RoutedEventArgs e)
{
    string playerName;
    string userInput = userInput.Text;

    Regex r = new Regex("^[a-zA-Z]*$");
    if (r.IsMatch(userInput))
    {
       ...

Upvotes: 2

Related Questions