Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 31 people (58%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I check an email address for valid domain and format?

Apr 7th, 2008 22:45
ha mo, Abolfazl Shirazi, Jim Owen, Abolfazl Shirazi


Abolfazl Shirazi
The answer of this question is quite easy . You can't
check an email address to see whether it is valid or
not . Because the user of your site can type every
things . BUT you can easily check that email address
for valid format and correct characters . for example
having @ symbol or not having bad characters or ....
The script below check email addresses for valid and
correct formats :
<html>
<head>
<script language="javascript" type="text/javascript">
function checkform() {
	text=document.myform.textfield.value
	// The Field Shouldn't be Empty
	if (text == "") {
		alert("Please Enter Your Email Address To Check .")
		return false
	}
	// Check For Bad Chars
	badchar = " ()<>\/[]{}+-*|;,'"
	for (i=0;i<text.length;i++) {
		for (j=0;j<badchar.length;j++) {
			if (text.charAt(i) == badchar.charAt(j)) {
				alert("Your Email Address May Has Some 
Bad Character or there is some space between letters")
				return false
			}
		}
	}
	// There Should Be a dot (.) Symbol
	if (text.indexOf(".") == -1) {
		alert("Your Email Address Doesnot have any dot(.) 
symbol")
		return false
	}
	// There Should Be an @ Symbol
	if (text.indexOf("@") == -1) {
		alert("Your Email Address Doesnot have any @ symbol")
		return false
	}
	else {
	// Only one @ symbol should be found
		symbolfound = 0
		for (i=0;i<text.length;i++) {
			if (text.charAt(i) == "@") {
				symbolfound++
			}
			else {
				continue
			}
		}
		if (symbolfound != 1) {
			alert("Ooops  Your Email Address Has More Than 
One @ Symbol")
			return false
		}
	}
	// Check For Syntax Errors Such as [email protected]  or   @.  or  ..
	if (text.indexOf("@.") != -1 || text.indexOf("[email protected]") != -1 || 
text.indexOf("..") != -1) {
		alert("Sorry Your Email Address has two symbol side by 
side and it is wrong")
		return false
	}
	// The first character shouldn't be @ or .
	if (text.charAt(0) == "@" || text.charAt(0) == ".") {
		alert("OH your first character seem to be . or @ and it 
is wrong")
		return false
	}
	// The last character shouldn't be @ or .
	if (text.charAt(text.length-1) == "@" || text.charAt
(text.length-1) == ".") {
		alert("OH your last character seem to be . or @ and it 
is wrong")
		return false
	}
	// The @ Symbol should be found before .
	foundchar1 = text.indexOf("@")
	foundchar2 = text.indexOf(".")
	if (foundchar1 > foundchar2) {
		alert("Sorry The @ Symbol should be found before dot(.) 
symbol but it isnt in yours")
		return false
	}
	alert("Perfect ! "+ text + " is a valid Email Address .")
	return false
}
</script>
</head>
<body>
<h1>Enter Your E-mail Address</h1>
<form name="myform" onsubmit="return checkform()">
<input type="text" name="textfield">
<p>
<input type="submit" name="submitbutton" value="Check">
<input type="reset" name="resetbutton" value="Reset"><p>
</form>
</body>
</html>
Enjoy It !!!
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com