Click here to Skip to main content
15,891,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, I would like to know the code for java script accepting only email address only in a text box, Helps me in my academic project
Posted

Yes you can use Javascript and Regex to accomplish your task,
here is an example

JavaScript
function validateEmail(elementValue)
{   
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;   
   return emailPattern.test(elementValue);   
}  


above function will return TRUE/FALSE depend upon result.
 
Share this answer
 
Comments
Tech Code Freak 31-Jan-12 0:53am    
5up!
C#
function validate(form_id,email) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {

      alert('Invalid Email Address');
      return false;
   }
}


XML
<form id="form_id" method="post" action="action.php" onsubmit="javascript:return validate('form_id','email');">

   <input type="text" id="email" name="email" />

   <input type="submit" value="Submit" />
</form>
 
Share this answer
 
Simli use regular expression control for this
 
Share this answer
 
C#
function isEmail() {
    if (window.parent.isMouseClicked) return;
    var regEmail, strValue;
    strValue = document.getElementById("txtEmail").value;
    regEmail =/^([A-Za-z]{1})([A-Za-z0-9_.\-]*)([A-Za-z0-9]{1})(@)([A-Za-z0-9]{1})([A-Za-z0-9_.\-]*)([A-Za-z0-9]{1})(\.)([A-Za-z]{1})([A-Za-z]*)$/

    strValue = document.getElementById("txtEmail").value;
    if (document.getElementById("txtEmail").value == "") return true;

    if(! regEmail.test(strValue)) {
        alert("Invalid Email Address.\n\n Please enter correct and complete EMail Address.");
        document.getElementById("txtEmail").value=""
        document.getElementById("txtEmail").focus();
        return false;
    }
    return true;
}



Add Onchange event to this function.

C#
<asp:textbox id="txtEmail" runat="server" onchange="return isEmail(this);" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 
v2
VB
FirstString = "me@me.com"
SecondString = "@"
position = InStr(FirstString, SecondString)
If position = 0 Then
MsgBox "Not a Valid email address: There was No @ Sign"
End If



Dim Email As String
Dim DotCom As String

Email = "me@me.con"
DotCom = Email.Substring(Email.Length - 4, 4)

If DotCom = ".com" Then
MsgBox("Ends in Dot Com")
Else
MsgBox("Doesn't End in Dot Com")
End If
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900