Jump to content
Sign in to follow this  

Javascript Tut - From Validation/Email Function

Recommended Posts

We'll start with the function that checks the email address text box. We'll only check if the text box has had anything typed into it. Checking for a correct email address is notoriously difficult, so we're just trying to keep things simple here.

Add the following function to your code (you won't be able to copy and paste because ours is an image - you'll learn more if you type it out for yourself):


Your script tags should now look like this:


The first line of the checkEmail function is this:

var email = document.frmOne.email.value;

Previously, we got a reference to a text box by using getElementById. Another way to access form elements is to use the following syntax:


Our form was called frmOne, so we start with document.frmOne. The element name you're trying to access comes next. In the HTML, we gave our text box element the name email. If you want to get what was in the text box, you need the value attribute at the end.

So we're getting the text value from the text box and storing it in a variable called email.

The IF statement tests what is in this variable. We're only testing for a blank value (two double quotes with no spaces between them, though you can single quotes instead of doubles). If email is blank then two things happen:

document.getElementById("email_error").innerHTML = "No Email Address";
return false;

We're using getElementById to access an HTML element with the ID "email_error". Notice that we're also using innerHTML after getElementById. The inner HTML is anything between two tags. So if you had <P>Some inner text</P> then the innerHTML would be "Some inner text". After an equal sign we're resetting the innerHTML and replacing it with the text "No Email Address".

The next line is return false. This is enough to set the whole of the function to false.

If the text box is not blank we set the innerHTML again, this time to a blank string. The return value is then true.

You can try your script out now. Don't type anything in the email text box. Just click the submit button. You should see this:


Now type something in the text box. Click the submit button again and the red error message should go away.


This was not created by me. Credits to the authors!

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

About us

MMO-Society is the Forum former known as Lordcraft, MMO-Society offers various of Resources to help you evolve your knowledge within Emulation, In the process we make sure to form a strong community bond in-between each other and thus we are proud to can admit that MMO-Society is possibly the friendliest Community Forum to currently exist.

Useful Quick-Links

  • Create New...