Require "proper" e-mail, in form field?

sdotbrucato

[H]ard|Gawd
Joined
Oct 7, 2005
Messages
1,722
Hi all, once again I'm stuck with a Javascript, Required Field issue.

CUrrently I have the following rule
Code:
	if (is_empty(form.email)== false)
	{
		alert("Please enter your E-mail.");
		form.email.focus();
		return false;
	}

I need this to make it so that the e-mail is filled out properly such as Name@server.com and can't be something like name@server, etc.

Any help would be great, whether the code or a reference that would help me, or hell even both.

Thanks

-S Brucato
 
I daresay it would be easier to google for a premade script perhaps, since this is a very common field/task.

But if it's for an assignment or whatnot...dunno, maybe some basic string/regex ops to check for *@*.(array of TLDs here), and maybe enforce minimum length for the user and domain.
 
I never have any luck looking for scripts on google, I guess I'm just not that talented. It's not for an assignment its for a personal website of mine that I am doing.

But I guess back to google I go.
 
Testing that a field has not been left blank is OK with javascript (it is 'helpful'). Testing about rather it has valid data or not is just silly. Javascript can not be relied on, it can be spoofed or simply turned off.

If you really want to validate some input (such as an email address), do it with what ever server side language you are using. It's the most effective way.

Now.... with that said, here is Google's 9,820,000 results for "javascript email validation". I can verify that at least the first 10 or so work to some effect (simple string searches to decent regex).
 
I've tried to put many of the ones in the google search into my JS, and it either doesn't submit, or it just doesn't validate. I don't know what I'm doing wrong, but it's getting really irritating. Any additional help would be greatly appreciated.


PS. Sorry if it comes off rude, I've just been racking my brain with this for two days now.

Anyway, thanks in advance.

-Steven
 
d3c1us said:
CUrrently I have the following rule
Code:
	if (is_empty(form.email)== false)
	{
		alert("Please enter your E-mail.");
		form.email.focus();
		return false;
	}

Your code is saying if the field is not empty, alert the user telling them to input an email.

it should be
Code:
if(is_empty(form.email))
 
buji said:
Your code is saying if the field is not empty, alert the user telling them to input an email.

it should be
Code:
if(is_empty(form.email))

No, that code is working fine for me. I need to make it so that it requires a "proper" email address. So the end-user just cant ender "lalalalala" as the email it has to be "name@server.tld"

And as for the RegEx, I'm not familiar with Javascript enough to know how to implement this at all. I've been coding with xHTML/CSS and could code and read it in my sleep. I've always stayed away from the back-end of web development and now its biting me in my ass.

Anyway, back to the research to find the answer.

Thank you to all replies and more are welcome. :)

-Steven
 
Back
Top