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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation bits into my form

Status
Not open for further replies.

sambkk

Technical User
Oct 27, 2008
1
TH
Hi all
I am hoping for some help on my form.
I would like to validate email field and a phone field.
currently the validation only checks if the fields have been left blank.
I would like to change it to a simple email validation that checks for a valid email adresss (simple is fine as (I know this can get technical) and the phone number to accept only numbers.

below is what I am using now

email:
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}

so instead of checking if the field is blank, I'd like to be able to check if the field IS NOT VALID, and then show error msg (which is defined elsewhere)

and the phone is here

var hphone = $("input#hphone").val();
if (hphone == "") {
$("label#hphone_error").show();
$("input#hphone").focus();
return false;
}

Now please note that I am not that familiar with javascript as I only "get my hands dirty" when I absolutely need to and apply some simple scripts I find on the net, so if your answer is very c0omplex it will go over my head.
Thanks in advance

Sami
 
This is a very good email validator, that matches most valid email addresses that others don't, e.g.:

"some@name"@address.com

I converted it from the PHP script here:
(you can read more about all the interesting rules at that URL)

Code:
function validEmailAddress(email) {
	if (typeof(email) != 'string') return false;

	var atIndex = email.lastIndexOf("@");
	if (atIndex == -1) return false;

	var domain = email.substr(atIndex + 1);
	var local = email.substr(0, atIndex);
	var localLen = local.length;
	var domainLen = domain.length;
	if (localLen < 1 || localLen > 64) {
		// local part length exceeded
		return false;
	} else if (domainLen < 1 || domainLen > 255) {
		// domain part length exceeded
		return false;
	} else if (local.charAt(0) == '.' || local.charAt(localLen-1) == '.') {
		// local part starts or ends with '.'
		return false;
	} else if (local.match(new RegExp('\\.\\.'))) {
		// local part has two consecutive dots
		return false;
	} else if (!domain.match(new RegExp('^[A-Za-z0-9\\-\\.]+$'))) {
		// character not valid in domain part
		return false;
	} else if (domain.match(new RegExp('\\.\\.'))) {
		// domain part has two consecutive dots
		return false;
	} else if (!local.replace(/\\\\/g, "").match(new RegExp('^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$'))) {
		// character not valid in local part unless
		// local part is quoted
		if (!local.replace(/\\\\/g, "").match(new RegExp('^"(\\\\"|[^"])+"$'))) {
		return false;
	}
	} else if (!domain.match(new RegExp('[\\S]+\\.[\\S]+'))) {
		// Not part of the RFC, but for our purposes, make sure the domain isn't 'local' (i.e. at least 'x.y', not just 'x')
		return false;
	}

	return true;
}

Note the last "// Not part of the RFC" comment - we didn't want to let through local addresses, and you may want to do the same if this is not for intranet use.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top