0

Hi all,

I have form validation working well. The following is how i currently validate emails.

My question is how can i amend to validate it as an email?

if(""==document.forms.fa.user_email.value)
{
alert("Please enter an email address.");
return false;
}

I have found this following script but my javascript syntax is not up to scratch

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;
   }
}
4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by rajendra.parmar
0

Hi all,

I have form validation working well. The following is how i currently validate emails.

My question is how can i amend to validate it as an email?

if(""==document.forms.fa.user_email.value)
{
alert("Please enter an email address.");
return false;
}

I have found this following script but my javascript syntax is not up to scratch

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;
   }
}

pass the id of the appropriate field where the email is entered.

function validate(textFieldId)
{
	//regex for email validation
	var regex = new RegExp("^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$", 'g');
	//test if the regex match
	if(document.getElementById(textFieldId).value.match(regex))
        {
                alert("Invalid email adress");
                return false;
        } else { return true; }
}

Didn't test this out, but this should work, or be close to solution, hope it helps :)

0

Here's another solution, I use this and now works great

Description:
1. Check only if is mail.
CheckMail('testname@testservice.com', '-1')
Will be checked if is mail (string @ string . string)

2. Check if is mail and if the mail service of the mail is allowed
CheckMail('testname@testservice.com', arrayOfAllowedServices)

// Array of allowed mail services
var services = new Array();
services[0] = "gmail"
services[1] = "yahoo"
services[2] = "hotmail"
CheckMail('testname@testservice.com', services)
// Will be checked if is mail (string @ string . string) and if the entered mail service is allowed.
// JavaScript Document - validator.js
 
/*
	@Author SkyDriver - Damjan Krstevski
	@Country/City: Macedonia, Skopje
	@Date 27/2/2010 - 7:33 AM
	@Description: Validation library...
	@License: Freeware, you can use, change and redistributed this library.
*/

/// <summary> Validate e-Mail address and e-Mail service </summary>
/// <param name="mail"> The e-Mail for validation </param>
/// <param name="allow"> Allowed e-Mail services (If allow = "-1" then all servies will be allowed) </param>
/// <api> CheckMail('testaddress@testservice.com', '-1'); </api>
function CheckMail(mail, allow) {
	try {
		if(mail.indexOf("@") != -1) {
			var len = mail.length;
			var index = mail.indexOf("@");
			if(len > index) {
				var tmp = mail.substring(index + 1, len);
				if(tmp.indexOf(".") != -1 && tmp.substring(tmp.indexOf(".")+1, tmp.length).length > 1) {
					if(allow != "-1") {
						var service = tmp.substring(0, tmp.indexOf("."));
						var arLen = allow.length;
						for(var i=0; i<arLen; i++) {
							if(service == allow[i]) {
								return true;	
							}
						}
						alert("You entered e-Mail address with invalid service!");
						return false;
					} else {
						return true;
					}
				} else {
					alert("You entered e-Mail address with invalid service!");
					return false;
				}
			} else {
				alert("You entered invalid e-Mail address!");
				return false;
			}
		} else {
			alert("Your e-Mail is not a valid!");	
			return false;
		}
	} catch (e) {
		alert("An error occurred.\nDetails: " + e);
		return false;
	}
}

Edited by Krstevski: n/a

0

Hi all,

I have form validation working well. The following is how i currently validate emails.

My question is how can i amend to validate it as an email?

if(""==document.forms.fa.user_email.value)
{
alert("Please enter an email address.");
return false;
}

I have found this following script but my javascript syntax is not up to scratch

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;
   }
}

above code is right.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.