Tenho um formulário para preenchimento de alguns dados através de Textbox
.
Tenho uma classe quem tem algumas funções de validação de campos nome, senha, usuário por exemplo. E em meu Code Behind
apenas chamo a função que valida passando os parâmetros para ela. Se cada campo digitado for válido uma string
declarada recebe o valor de ok.
Gostaria que no final após todos os campos serem validados o botão salvar que está desabilitado ficasse habilitado.
Vejam como estou fazendo:
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);
}
Minha classe 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;
}
Leave
?txt_nome
,txt_dtNascimento
e as subsequentes.