Ajaxorized

Realtime email validation with scriptaculous

October 18, 2007

When adding this tiny javascript to the onkeyup event of your inputbox, the border will turn green when the email address. It uses scriptaculous' morph function to perform this transformation. See a demo

The inputbox (HTML):

 
<input type = "email"  id = "myinput" onKeyUp = "validateEmail(this)"  />
 

The event handler (javascript):

 
var isValid = false;
 
validateEmail = function(e) {
  if(/^[a-zA-Z][w.-]*[a-zA-Z0-9]@[a-zA-Z0-9][w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z]$/.test(e.value)) {
    if(!isValid) {
      $(e).morph('border-color:#00FF00', {duration:.3});
      isValid = true;
    }
  } else {
    if(isValid) {
      $(e).morph('border-color:#FF0000', {duration:.3});
      isValid = false;
    }
  }
}
 

Add to: del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Windows Live Yahoo
topFiled under: Ajaxorized, Scriptaculous, Prototype, Javascript, HTML | Willem @ 12:42 pm |

5 Comments

RSS feed for comments on this post. TrackBack URL

  1. Demo seems to fail in IE6.

    Comment by Rich — October 18, 2007 @ 3:44 pm

  2. Hi Rich,
    You’re right, I fixed it.

    Thanks!

    Comment by Willem — October 18, 2007 @ 6:04 pm

  3. Excellent compact idea you have developed here. Seems like the underscore character is missing from the range in your ‘if’ statement. I see many people’s email addresses that include this character. Thought I might bring it to your attention.

    Comment by Chris — February 6, 2008 @ 5:15 pm

  4. Do an email address really need to start with a letter?
    I remember those old compuserve address which started with a number …

    e.g. 112523.3273@compuserve.com

    Comment by JFSebastian — February 27, 2008 @ 11:57 am

  5. Realtime email validation with scriptaculous…

    When adding this tiny javascript to the onkeyup event of your inputbox, the border will turn green when the email address. It uses scriptaculous’ morph function to perform this transformation….

    Trackback by Bookmarks.WittySparks.com — May 27, 2008 @ 4:40 pm

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word