Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating the @ sign and a dot through a function.

Status
Not open for further replies.

salimwng

IS-IT--Management
Mar 11, 2002
134
MU
Hi,

I have worked out the following coding to check the presence of an @ sign in a form when being filled in. The program works correctly. However i need that it checked for the presence of a . ( dot ) also. Can anybody help me please....i've been strunggling with this since 2 days.

I searched also in the forums, but the coding is too advanced. I was wondering if their is a coding similar to mine, listed below;

CODING OF FUNCTIONS PART;
=========================

function doemail()
{
if(document.form1.email.value.search("@")<0||document.form1.email.value.length==1)
{
alert("Email address is wrong. Please check !")
return false
}
else
{
return true
}

CODING OF FORM PART
===================

<tr>

<th align="left" width="279">Email Address</th>
<td width="461"><input type="text" name="email" size="30" maxlength="25" onChange="return doemail();"></td>

</tr>

================================
Can anybody guide me please,

Thank you
Salim
 
Why not use some "regex" code to perform your validation. The following is something I have used regularly:
Code:
function formValidateEmail(email) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(email);
}
function formValidate(obj) {
	if (!formValidateEmail(obj.useremail.value)) {
		alert("Email address is wrong.  Please check !")
		return false;
	}
}
...
<form onsubmit="return formValidate(this)" ... >
...
It should validate any email address you throw at it. [thumbsup2]

Hope this helps.

Pete.


Web Developer & Aptrix / IBM Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thank you for your reply Pete. I do appreciate your prompt help.

However, due to it's complexity i cannot put it in my project for the moment. I am looking the most simplest way of doing it, { though i know the shortest the coding, the better it is }. But got no choice here.

Let me know if you know something.

Thanks again Pete.
Salim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top