Go Premium for a chance to win a PS4. Enter to Win


Very Simple email validation onchange?

Posted on 2004-10-14
Medium Priority
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 'fred@fred.com') 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 400 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 javascriptsource.com
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!! http://javascript.internet.com -->

<!-- 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) {
form.info.value = "Browser Information: " + navigator.userAgent;
function getValue(element) {
var value = getCookie(element.name);
if (value != null) element.value = value;
function setValue(element) {
setCookie(element.name, 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 (form.email.value.indexOf("@") == -1 ||
form.email.value.indexOf(".") == -1) {
fixElement(form.email, "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 = "mailto:youremailaddress.com" 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="http://javascriptsource.com">The JavaScript Source</a></font>


Accepted Solution

Kupi earned 1600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), 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 …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
Suggested Courses

971 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