Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:28:43+00:00 2026-05-26T15:28:43+00:00

All I want to be able to do is validate an email every time

  • 0

All I want to be able to do is validate an email every time the user enters a new character into the input field. When the input field is empty, it shouldn’t show anything, but when it contains letters, it should show where the invalid characters are.

My code works but doesn’t show nothing when the input field is empty because it uses the nested ‘else’ instead of the one it should use. anyone help? thanks in advance. andyy

var tick = "<img src='images/tick.png' width='20' height='20'/>";
var cross = "<img src='images/cross.png' width='20' height='20'/>";
var email_element = document.contact_form.email_field.value;
function validate_email(id) {
     if ( email_element != '' ) {
        var letters = /^[A-Za-z]+$/;
        if ( document.contact_form.email_field.value.match(letters) )
        {
            document.getElementById(id).innerHTML = tick;
                valid = true;
        } else {
            document.getElementById(id).innerHTML = cross;
            valid = false;
        }
    } else {
        document.getElementById(id).innerHTML = '';
    }
    return valid;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:28:43+00:00Added an answer on May 26, 2026 at 3:28 pm

    The problem is most likely this line:

    var email_element = document.contact_form.email_field.value; 
    

    You are assigning a global variable equal to the value of the email field, and then never updating that variable again. This does not create an “active” link to the current value of the field, it just stores the value as it was when that line was executed. Which in turn means that the first if statement in your function, if ( email_element != '' ), is evaluating a non-current value of the email field.

    Move that line to be the first thing inside your function and then every time the function runs you’ll get the latest (current) value of that field.

    EDIT: Also, you are not assigning a value to valid in the non-nested else. You should declare valid as a local variable in the function and be sure to set it appropriately in every if and else branch (or default it to false when you declare it). As thomasrutter
    said you are not currently declaring valid with the var statement in your function, which means it will be created as a global variable, which in turn means that when you don’t give it a value in your non-nested else your function will return whatever value valid already had from the previous call. (And really, based on the code you’ve posted, you don’t need the valid variable at all: you could just say return true; or return false; directly inside each branch of your if/else structure.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building the world's simplest library application. All I want to be able to
All I want is to be able to change the color of a bullet
I want to be able to log all JMX data accessible via jconsole. Is
I want to be able to match against all elements in a given context
Currently, I have: outByte.writeInt(0x49492a00); outByte.writeInt(0x08000000); But i want to be able to write all
I have a class Employee. I want to be able to Validate() it before
All I want is to update an ListViewItem's text whithout seeing any flickering. This
All I want is a pragmatic random number generator in C# so I can
All I want is to get the website URL. Not the URL as taken
All I want is a one pixel black border around my white UILabel text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.