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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

821 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