How to Validate Email in JavaScript with Regular Expression

JavaScript Email Validate
We have seen many Regular Expression tutorials in VB.NET and C-sharp, this is our first JavaScript tutorial on Regular Expressions. So today we’ll see how to validate an email address that's being entered to a textbox. We have a textbox and a button, when you enter something into the textbox and click on the “validate”  button it’ll tell if It’s an email address or not(being noob GRRR!).

Add our JavaScript first(make sure you add it to the head) :

Next add the input box and the validate button :

That’s all. You can use this for some kind of subscription button or register/login forms. I hope this little snippet is helpful for you.

How To Validate Textbox : Only Numbers, Only Characters, Not Null, Only Email in VB.NET , textbox

Today I have brought you something delicious :) so you guys wonder how it works , first we have to create a module and we just have to assign the textbox for a validation (Only Number,Only Characters,Not Null,Only Email) so first of all lets create a new module and add this codes

Imports System.Text.RegularExpressions
Module Module1
    Public Enum ValidationType
        Only_Numbers = 1
        Only_Characters = 2
        Not_Null = 3
        Only_Email = 4
    End Enum
    Public Sub AssignValidation(ByRef CTRL As Windows.Forms.TextBox, ByVal Validation_Type As ValidationType)
        Dim txt As Windows.Forms.TextBox = CTRL
        Select Case Validation_Type
            Case ValidationType.Only_Numbers
                AddHandler txt.KeyPress, AddressOf number_Leave
            Case ValidationType.Only_Characters
                AddHandler txt.KeyPress, AddressOf OCHAR_Leave
            Case ValidationType.Not_Null
                AddHandler txt.Leave, AddressOf NotNull_Leave
            Case ValidationType.Only_Email
                AddHandler txt.Leave, AddressOf Email_Leave
        End Select
    End Sub
    Public Sub number_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim numbers As Windows.Forms.TextBox = sender
        If InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
            e.KeyChar = Chr(0)
            e.Handled = True
        End If
    End Sub
    Public Sub OCHAR_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If InStr("1234567890!@#$%^&*()_+=-", e.KeyChar) > 0 Then
            e.KeyChar = Chr(0)
            e.Handled = True
        End If
    End Sub
    Public Sub NotNull_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim No As Windows.Forms.TextBox = sender
        If No.Text.Trim = "" Then
            MsgBox("This field Must be filled!")
        End If
    End Sub
    Public Sub Email_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Email As Windows.Forms.TextBox = sender
        If Email.Text <> "" Then
            Dim rex As Match = Regex.Match(Trim(Email.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
            If rex.Success = False Then
                MessageBox.Show("Please Enter a valid Email Address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Email.BackColor = Color.Red
                Exit Sub
                Email.BackColor = Color.White
            End If
        End If
    End Sub
End Module

now lets test the module i mean lets add 4 textboxes and assign each for a validation and see whether it works !

add 4 textboxes  now add labels for each like this

validate textbox in

you can only enter numbers in textbox1
you can only enter characters in textbox2
you cannot leave textbox3 blank
you can only enter email in textbox4

now go to form_load event and add these codes


        AssignValidation(Me.TextBox1, ValidationType.Only_Numbers)
        AssignValidation(Me.TextBox2, ValidationType.Only_Characters)
        AssignValidation(Me.TextBox3, ValidationType.Not_Null)
        AssignValidation(Me.TextBox4, ValidationType.Only_Email)

thats where we assign the textbox for a validation !!

now lets test it just debug and try entering characters in textbox1 you cannot enter , go to textbox2 try entering characters you cannot enter , go to textbox3 and without entering anything go to textbox4 a message box will popup now go to textbox4 and enter something and click into another textbox and he textbox4 will be red in color and you will get a message

thanks for reading hope this is useful ! share ! comment !