Hello,

I have made a web application program that takes one string and outputs it through an array into three seperate textboxes using comma seperated values.

This works fine - BUT I want data 0 to only allow entry of strings (it's a name),
data 2 to only allow entry of numbers (account number),
and data 3 to only allow entry of a string (password).

What I'm trying to work out is, if I have to somehow seperate every part with try catches or is there some more obvious and efficient way of dealing with errors in this case.

(If any of the above is nonsense - that is because I don't totally know what I am doing:)) Any advice gratefully received - thank you.

Here's the code

private void parseBut_Click(object sender, EventArgs e)
        {
            String namaccpas;
            ////Create the arrays to hold the data and the comma delimiter
            String[] data = new String[3];
            char[] chars = new char[] { ',' };

            //Use textboxes to show records one at a time from the data array
            namaccpas = namaccpasTextBox.Text;
            data = namaccpas.Split(chars);

            namTextBox.Text = (data[0]);
            accTextBox.Text = (data[1]);
            pasTextBox.Text = (data[2]);
        }

When you say the name is a string, do you have a list of allowable (or not allowable) characters? How many digits is the account number (depending on length there are different methods to check if it is all digits). Same for the password, what characters are allowed?

When you say the name is a string, do you have a list of allowable (or not allowable) characters? How many digits is the account number (depending on length there are different methods to check if it is all digits). Same for the password, what characters are allowed?

Hello ... thanks for your interest

For now, the length of the input is not so important (even though it is of interest to know how to do that too for the future as it obviously sounds very useful).

All I'm trying to do for now though is get an error message to show if a number is entered when a word is required (name and password), and a word is entered when a number is required (account number).

All the best .... John

Well the easiest is to use Decimal.TryParse:

Decimal value;
if (Decimal.TryParse(data[0], out value)) {
    // data[0] is a number
} else {
    // data[0] is not a number
}

This will work for numbers up to 20 digits in length. Otherwise

public Boolean IsNumber(String value) {
    Boolean result = true;
    foreach (char c in value) {
        if (char.IsNumber(c) == false) {
            result = false;
            break;
        }
    }

    return result;
}
Comments
Great answer and help for the future too

Thank you - Not only have you solved my current quandry - but most likey another that I am likely to encounter in the future.

Thanks again ... I'm off to make that work now

This question has already been answered. Start a new discussion instead.