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 7747893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:38:38+00:00 2026-06-01T10:38:38+00:00

Okay so my code pretty much works… except if you click submit a second

  • 0

Okay so my code pretty much works… except if you click submit a second time and there is STILL a field empty, the appended * required in the label tags before the input tags will become duplicated. This is bad! I am able to alert the nodeValue after function(x), yet somehow there is an issue with alerting anywhere else… which also, I believe, is the reason I cannot use the simple if statement solution I have commented out at the bottom of my code.

Any revisions to my code or suggestions will be greatly appreciated. Alternatively, I’d love some links to follow. Hopefully one day I can think programmatic enough to give back on SO…

Thanks so much in advance. Here is my code:

Javascript:

var obj = {


form : document.getElementsByTagName('input'),
btn : document.getElementById('submit'),

init : function(){

obj.btn.form.onsubmit = obj.handleEvent;

},

handleEvent : function(){

for (var i=0; i<obj.form.length; i++){

    if (obj.form[i].value===''){

    switch(obj.form[i].name){
        case 'fname' : 
        case 'lname' : 
        case 'phone' : 
        case 'email' : match=true; obj.error(obj.form[i]); break;  
        }   
    }
}
return false; 
},


error : function(x) {
    var req = document.createTextNode(' * required');
    var span = x.previousSibling.appendChild(document.createElement('span'));
    span.className = 'error';
 //     if (x.previousSibling.firstChild.nextSibling.firstChild.nodeValue !== ' *    required'){
        span.appendChild(req);
  //        }

  //      alert(x.previousSibling.firstChild.nextSibling.firstChild.nodeValue);
   }   
 }

and the HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<link type="text/css" href="css/main.css" rel="stylesheet" />

<head>
    <title>JavaScript Input form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <form method="post" action="">
        <div>
            <ul>
                <li><label>First Name</label><input type="text" name="fname" size="30" /></li>
                <li><label>Last Name</label><input type="text" name="lname" size="30"  /></li>
                <li><label>Phone</label><input type="text" name="phone" size="30"  /></li>
                <li><label>Email</label><input type="text" name="email" size="30" /></li>
                <li><input type="submit" name="submit" id="submit" value="Submit" />
           </ul>
      </div>
      </form>
 <script type="text/javascript" src="input.js"></script>
 <script type="text/javascript">obj.init();</script>
 </body>
 </html>

Again thank you for reading !

  • 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-06-01T10:38:39+00:00Added an answer on June 1, 2026 at 10:38 am

    Simply empty out your span before you validate your input fields.

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

Sidebar

Related Questions

Okay I have updated my code a little, but I am still not exactly
Okay, so, this code is pretty basic. The user inputs an answer into a
Okay so I am pretty much doing a crash course through creating a java
Okay, I'm reading about Linux kernel development and there are some code snippets using
I'm connecting Java to Oracle database, everything's is going pretty okay, except the following
Okay I have updated my code quite a bit. I am getting a new
Okay, I have this code: <script language=javascript type=text/javascript> <!-- function requestPage(page) { var request
Okay, so i have this code i wrote: class Connection { public static StreamWriter
Okay, so I've got the following code shown below to create a dialog using
Okay so, I'm currently running this code to move a bunch of data from

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.