I have a form for filling some data with Textbox
.
I have a class that has some validation functions for fields name, password, user for example. And in my Code Behind
I just call the function that validates by passing the parameters to it. If each entered field is valid a string
declared receives the value of ok .
I would like the end button after all the fields to be validated, the save button that is disabled is enabled.
See 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;
}