Very Simple email validation onchange?

Posted on 2004-10-14
Last Modified: 2012-08-13
I need a very simple email validation script (so simple I can't do it :-( ). can u help?
I have this input field:

<input name="register_email" type="text" size="30" maxlength="50" onblur="this.value=this.value.replace(/ /g,'_')" onfocus="change2Msg(2);" value="<cfif isdefined('url.em')><cfoutput>#url.em#</cfoutput></cfif>">

I need to FORCE the user to stay in the input field if no '@' or '.' is part of the entered string.  I'm sure this is goofy but it would help me with a very simple task I need.

Does this make sense?

In otherwords ... when I tab (or click) into this text field 'register_email' and I fail to enter a text string that contains an '@' and an '.' (i.e. fred instead of '') then when I leave the field ... force the user BACK into the text input field.

(i need to keep that onfocus="change2Msg(2);" code in my line and the coldfusion output field for value.)

Thanks for any help!
Question by:rcbuchanan

Assisted Solution

reginab earned 100 total points
ID: 12310731
well this is the simplest way
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

then just focus back to the input field

here is a little more detail from
should get you started.


   1.  Paste the first coding into the HEAD of your HTML document
   2.  Put the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Copy this code into the HEAD your HTML document  -->


<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->

<!-- Begin
function getCookie(name){
var cname = name + "=";              
var dc = document.cookie;            
if (dc.length > 0) {              
begin = dc.indexOf(cname);      
if (begin != -1) {          
begin += cname.length;      
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
return null;
function setCookie(name, value) {
var now = new Date();
var then = new Date(now.getTime() + 31536000000);
document.cookie = name + "=" + escape(value) + "; expires=" + then.toGMTString() + "; path=/";
function getInfo(form) { = "Browser Information: " + navigator.userAgent;
function getValue(element) {
var value = getCookie(;
if (value != null) element.value = value;
function setValue(element) {
setCookie(, element.value);
function fixElement(element, message) {
function isMailReady(form) {
var passed = false;
if (form.fullname.value == "") {
fixElement(form.fullname, "Please include your name.");
else if ("@") == -1 ||".") == -1) {
fixElement(, "Please include a proper email address.");
else if (form.message.value == "") {
fixElement(form.message, "Please include a message.");
else {
passed = true;
return passed;
// End -->

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->


<FORM NAME = "mail" ACTION = "" METHOD = "POST" ENCTYPE = "text/plain" onSubmit = "return isMailReady(this);">
<INPUT TYPE = "hidden" NAME = "info">
Your Name:
<INPUT TYPE = "TEXT" NAME = "fullname" onFocus = "getValue(this)" onBlur = "setValue(this)">
Your Email Address:
<INPUT TYPE = "TEXT" NAME = "email" onFocus = "getValue(this)" onBlur = "setValue(this)">
<TD COLSPAN = "2">
Enter your Message:
<TEXTAREA ROWS = "8" COLS = "38" NAME = "message">
<TD COLSPAN = "2">
<INPUT TYPE = "SUBMIT" VALUE = " Submit ">
<INPUT TYPE = "RESET" VALUE = " Cancel ">

<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="">The JavaScript Source</a></font>


Accepted Solution

Kupi earned 400 total points
ID: 12310990
What about this?

<input name="register_email" type="text" size="30" maxlength="50" onblur="if (!(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/).test(this.value)) this.focus();">

This regular expression will validate the e-mail address and focus back the field if not correct.


Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now