1
Good afternoon, I have a function that checks if the typed email exists in the bank, and if the email is valid. Follow the function:
<script type = "text/javascript" >
function validateEmail(emailField) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (reg.test(emailField.value) == false) {
alert('Email inválido.');
emailField.value = '';
return false;
}
document.getElementById("btnValidaEmail").click()
return true;
}
</script>
This is the html of the txtemail field:
<asp:TextBox ID="txtEmail" runat="server" class="form-control" onChange="validateEmail(this);"></asp:TextBox>
The Validate email button:
<asp:Button ID="btnValidaEmail" runat="server" Text="Button" Style="display: none" OnClick="btnValidaEmail_Click" />
And the function that is in btnValidaEmail:
clslogin pegaid = new clslogin();
SqlConnection conConexao3 = clsdb.AbreBanco();
if (txtid.Text != "") {
clslogin log = new clslogin();
SqlCommand cmd3 = new SqlCommand("SELECT email from pessoa where email ='" + txtEmail.Text + "' and id != '" + txtid.Text + "'", conConexao3);
SqlDataReader dr3 = cmd3.ExecuteReader();
if (dr3.HasRows == true) {
if (dr3.Read()) {
veremail = true;
txtEmail.Text = "";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email já existe em outro cadastro.');", true);
}
}
} else {
clslogin log = new clslogin();
SqlCommand cmd3 = new SqlCommand("SELECT email from pessoa where email ='" + txtEmail.Text + "'", conConexao3);
SqlDataReader dr3 = cmd3.ExecuteReader();
if (dr3.HasRows == true) {
if (dr3.Read()) {
veremail = true;
txtEmail.Text = "";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email já existe em outro cadastro.');", true);
}
}
}
In a form that already has txtid, it works perfectly, but in a new form without data, when filling the email it only checks if the email is valid, and does not check if it exists in the system, it does not even enter the function, I put an Alert before if(txtid!="")And neither did Alert get in, any idea what might be going on? Thanks.
In this case, it’s as if I haven’t activated the
document.getElementById("btnValidaEmail").click()
, that’s it?– Sam
I don’t think that’s it @Pra, he’s working with webforms and trying to take the onchange event from the textbox component of webforms, only it’s a little different in webforms what he wants to do. I believe what she needs is this https://answall.com/a/260810/5846
– Pablo Tondolo de Vargas