I am performing regular expressions on a registration form using Web forms. I would like to know if it is well done because when registering nothing happens.
This is my regular expression code:
Dim expre_regul As String = "/^[a-zA-Z0-9\._-]+@(hotmail|gmail)[.][a-zA-Z]{2,4}$/"
Dim r As Regex = New Regex(expre_regul)
If Not r.IsMatch(txtCorreo.Text) Then
Return
End If
This is the complete code of my "Register" button
Protected Sub btnRegistrar_Click(sender As Object, e As EventArgs) Handles btnRegistrar.Click
Try
Dim clRegistrar As New SIMULADOR_LOG.clsExamenLOG
Dim User As New SIMULADOR_ENT.clsUsuarioSim
With User
.dni_user = txtDNI.Text
.ape_pat_user = txtApellidoPaterno.Text
.ape_mat_user = txtApellidoMaterno.Text
.nombre_user = txtNombres.Text
.direccion_user = txtDireccion.Text
.celular_user = txtCelular.Text
.correo_user = txtCorreo.Text
.dept_user = cmbDepartamento.Text
.prov_user = cmbProvincia.Text
.dist_user = cmbDistrito.Text
.fech_nacimiento = txtFechaNacimiento.Text
End With
Dim expre_regul As String = "/^[a-zA-Z0-9\._-]+@(hotmail|gmail)[.][a-zA-Z]{2,4}$/"
Dim r As Regex = New Regex(expre_regul)
If Not r.IsMatch(txtCorreo.Text) Then
Return
End If
Dim mensajeLog As String = clRegistrar.RegistrarUsuario(User)
Dim Mensaje As String = "<script type=""text/javascript"">" &
"Message('El sistema dice: " & mensajeLog & "')</script>"
ClientScript.RegisterStartupScript(Me.GetType, "msg", Mensaje)
Response.Redirect("SimuladorExamen.aspx")
Catch
'ScriptManager.RegisterStartupScript(Me, GetType(Page), "alerta", "alert('Ingrese un valor válido');", True)
End Try
End Sub
.net does not use delimiters in the regular expression. Delete the ones
/
at the beginning and at the end.Use https://regex101.com/ to test your regular expression.
For example, I used:
And validation tells me that "mail@ mail .com" does NOT match the pattern.
But, if I use:
Validation tells me that "mail@ hotmail .com" does match the pattern.