Solved

Very Simple email validation onchange?

Posted on 2004-10-14
2
636 Views
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!
Richard
0
Comment
Question by:rcbuchanan
2 Comments
 
LVL 4

Assisted Solution

by:reginab
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 javascriptsource.com
should get you started.


<!-- TWO STEPS TO INSTALL FORM VALIDATION:

   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  -->
             
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- 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) {
alert(message);
element.focus();
}
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 {
getInfo(form);
passed = true;
}
return passed;
}
// End -->
</SCRIPT>

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

<BODY>

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

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

0
 
LVL 3

Accepted Solution

by:
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.

Kupi
0

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