1
I have a form to fill in some data through Textbox
.
I have a class that has some validation functions of fields name, password, user for example. And in my Code Behind
just call the function that validates by passing the parameters to it. If each input field is valid a string
declared receives the value of okay.
I would like that at the end after all fields are validated the save button that is disabled was enabled.
Look how I’m doing:
string validacaoImg = "";
private void txt_nome(object sender, TextChangedEventArgs e)
{
string regex = "^[A-Za-záéíóúàâêôãõüçÁÉÍÓÚÀÂÊÔÃÕÜÇ ]+$";
Funcao.validaCampos(regex, txtNome.Text, imgNome, validacaoImg);
}
private void txt_dtNascimento(object sender, SelectionChangedEventArgs e)
{
string regex = @"\d{2,2}/\d{2,2}/\d{4,4}";
Funcao.validaCampos(regex, txtNascimento.Text, imgNascimento, validacaoImg);
}
private void txt_usuario(object sender, TextChangedEventArgs e)
{
string regex = "^[[email protected]]+$";
Funcao.validaCampos(regex, txtUsuario.Text, imgUsuario, validacaoImg);
}
private void txt_senha(object sender, TextChangedEventArgs e)
{
string regex = @"(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[A-Za-z0-9].+$";
Funcao.validaCampos(regex, txtSenha.Text, imgSenha, validacaoImg);
}
My class Função
:
public static void validaCampos(string regex, string txt, Image img, string validacaoImg)
{
TextBox texto = new TextBox();
texto.Text = txt;
img.Visibility = Visibility.Visible;
Regex rx = new Regex(regex);
if (texto.Text.Length > 0)
{
if (validaTextBoxes(txt, rx))
{
texto.BorderBrush = Brushes.Green;
validacaoImg = "check";
texto.ToolTip = null;
}
else
{
texto.BorderBrush = Brushes.Red;
texto.ToolTip = "Fora do padrão!";
validacaoImg = "errado";
}
}
else
{
texto.BorderBrush = Brushes.Red;
texto.ToolTip = "Este campo não pode ficar vazio!";
validacaoImg = "errado";
}
validarImagem(img, validacaoImg);
}
public static bool validaTextBoxes(string texto, Regex regex)
{
bool isValid = regex.IsMatch(texto);
if (isValid)
{
return true;
}
else
{
return false;
}
}
public static void validarImagem(Image img, string validacaoImg)
{
var path = System.IO.Path.Combine(Environment.CurrentDirectory, "Imagens/");
var uri = new Uri(path + validacaoImg + ".jpg");
BitmapImage bm = new BitmapImage(uri);
img.Source = bm;
}
The first block of code is called when? No
Leave
?– Jéf Bueno
@Jéfersonbueno you refer to which block of the Class I created Funcao or what is in the XAML Codebehind?
– Wesley Heron
I mean the functions
txt_nome
,txt_dtNascimento
and subsequent.– Jéf Bueno
@Jéfersonbueno yes
– Wesley Heron