E
E
Evgeny Semashko2020-04-22 10:20:35
C++ / C#
Evgeny Semashko, 2020-04-22 10:20:35

How to implement input validation on button click?

I want to implement a validation of all input data in the registration fields and after clicking on the Registration button, so that all input data is checked for correctness in the method and then there is a transition to the Login form.

I have the correctness checks themselves, but I want to implement so that when you click on the button, data is entered if all the fields are correctly filled in and there is a transition to another form.

I tried to hack through a boolean variable, but the crutch did not pass

The project itself The project

is made on EF Core using MS Sql

Ps The code turned out to be too cumbersome and does not let it in here, because there are too many characters.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2020-04-22
@evgenysemashko

I want to implement validation of all input data in the registration fields and after clicking on the Registration button
the train of thought is clear, typical for a beginner.
but I ’ll explain not the best :
to check the correctness of the entered data in any fields , it is common to do validation (by reference, a special case. All this is in WinForms, and in WPF, and in UWP, and in the web interface ) after entering, or even as you enter .
I did not look at the code at all, due to an initially unsuccessful approach to the problem of checking the correctness of upd user input . there is a well-established term for this process - validation.
upd well, and the well-established tricks that, in the case of C #, Microsoft, kindly provides us with what is called out of the box, for most major desktop application development scenarios

E
Evgeny Semashko, 2020-04-23
@evgenysemashko

Here is the solution for one of the boxes

public bool IsNameValid(string name)
        {
            bool valid = false;
            Regex check = new Regex(@"^([A-Z][a-z-A-z]+)$");
            valid = check.IsMatch(name);

            if (valid == true)
            {
                return valid;
            }
            else
            {
                return valid;
            }
        }

        private void nameBox_TextChanged(object sender, EventArgs e)
        {
            TextChange(sender, e);
        }

        private void nameBox_Validating(object sender, CancelEventArgs e)
        {
            if (!IsNameValid(nameBox.Text))
            {
                inputincorrect.SetError(nameBox, "Invalid name");
            }
            else
            {
                inputincorrect.SetError(nameBox, null);
            }
        }

The check itself in the button
if (IsNameValid(nameBox.Text) == true && // ещё проверки)
           {
                // какой-то код
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question