HilarYo Posted January 17, 2016 at 05:51 PM Report Share #592108 Posted January 17, 2016 at 05:51 PM Boas tardes, Estou a tentar usar o Custom Validator para validar um numero de contribuinte, e tenho os seguintes códigos: Pagina .aspx : <tr> <td style="vertical-align: middle;"> <b>NIF:</b> </td> <td> <asp:TextBox ID="TextBoxNIF" runat="server" CssClass="form-control" CausesValidation="true"></asp:TextBox> </td> <td> <asp:CustomValidator ID="CustomValidatorNif" runat="server" ErrorMessage="NIF Inválido" ControlToValidate="TextBoxNIF" Text="*" SetFocusonerror="True" OnServerValidate="CustomValidatorNif_ServerValidate" ValidateEmptyText="True"></asp:CustomValidator> </td> </tr> Código .cs protected void CustomValidatorNif_ServerValidate(object source, ServerValidateEventArgs args) { String a = args.Value.ToString(); if (ValidarNif(a)) { args.IsValid = true; } else { args.IsValid = false; } } //Função Valida NIF public bool ValidarNif(string Contrib) { bool functionReturnValue = false; functionReturnValue = false; string[] s = new string[9]; string Ss = null; string C = null; int i = 0; long checkDigit = 0; s[0] = Convert.ToString(Contrib[0]); s[1] = Convert.ToString(Contrib[1]); s[2] = Convert.ToString(Contrib[2]); s[3] = Convert.ToString(Contrib[3]); s[4] = Convert.ToString(Contrib[4]); s[5] = Convert.ToString(Contrib[5]); s[6] = Convert.ToString(Contrib[6]); s[7] = Convert.ToString(Contrib[7]); s[8] = Convert.ToString(Contrib[8]); if (Contrib.Length == 9) { C = s[0]; if (s[0] == "1" || s[0] == "2" || s[0] == "5" || s[0] == "6" || s[0] == "9") { checkDigit = Convert.ToInt32( c) * 9; for (i = 2; i <= 8; i++) { checkDigit = checkDigit + (Convert.ToInt32(s[i - 1]) * (10 - i)); } checkDigit = 11 - (checkDigit % 11); if ((checkDigit >= 10)) checkDigit = 0; Ss = s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] + s[7] + s[8]; if ((checkDigit == Convert.ToInt32(s[8]))) functionReturnValue = true; } } return functionReturnValue; } Já pesquisei por todo o lado, e ainda não consegui resolver o problema, dá ideia que não executa a função, alguém pode dar uma ajudinha? Obrigado, Cumprimentos Link to comment Share on other sites More sharing options...
HilarYo Posted January 17, 2016 at 10:35 PM Author Report Share #592119 Posted January 17, 2016 at 10:35 PM Já consegui resolver o problema, e não sei bem como porque já tinha experimentado, e agora de um momento para o outro funcionou, a resolução foi: no evento do botão responsável por submeter o formulário usar o seguinte codigo: if (Page.IsValid) { PanelRegistarCliente.Visible = false; PanelInserirMorada.Visible = true; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now