I was wondering if anyone could give me the code for email validation on a textbox named, txtEmail.

I would like to inform the user that they have not entered a valid email address via a message box and not allow them to proceed until it is correct..

Regular expression validators do not work when i host it online so they are out of the question..

Can anyone help me with this??

thanks

Eric

Regular expression validators do not work when i host it online so they are out of the question..

I cant understand your point. Could you please explain a bit more. Since without reg expression it will be a bit difficult but doable by string parsing.

Hi,

I had a recent issue (before moving to a VPS server) where my asp.net validation controls would not work when uploaded to the ISPs website. This is a setup problem their end and should be configurable.

If however they are not prepared to do this then have you thought about the option of using a regular expression within a javascript function. You could then attach a client side event to the button / text box and return the result from javascript and perform a post back depending on the result. Personally I would do this in javascript and server-side in order to gain maximum security benefits.

If you need any help on adding client side attributes to a text box then let me know.

Cheers

Do it in Javascript. Function is given below :

function checkEmail (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert('"' + emailStr + '" is not a valid email address.\n\n Please make sure to input a valid email address.')
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
   alert('"' + emailStr + '" is not a valid email address.\n\n Please make sure to input a valid email address.')
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid.")
		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name you entered doesn't seem to be valid.")
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   alert("The address must end in a three-letter domain, or two letter country code.")
   return false
}

if (len<2) {
   var errStr="This address is missing a hostname."
   alert(errStr)
   return false
}

return true;
}

Looking at the javascript above:
This won't detect 4 character TLDs which are valid, such as .mobi or .info:

if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   alert("The address must end in a three-letter domain, or two letter country code.")

Here is the function I use for validation but it is server side:

public static bool IsValidEmailAddr(string email)
    {
      try
      {
        System.Net.Mail.MailAddress addr = new System.Net.Mail.MailAddress(email);
      }
      catch
      {
        return false;
      }
      return true;
    }

Try this javascript.

<script language = "Javascript">


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
</script>

<form name="frmSample" method="post" action="#" onSubmit="return ValidateForm()">
                <p>Enter an Email Address : 
                  <input type="text" name="txtEmail">
                </p>
                <p> 
                  <input type="submit" name="Submit" value="Submit">
                </p>
              </form>

Do you want it in server side ??

This article has been dead for over six months. Start a new discussion instead.