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

  • Home
  • SEARCH
  • 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 6576043
In Process

The Archive Base Latest Questions

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

script : window.onload = initForms; function initForms() { for (var i=0; i< document.forms.length; i++)

  • 0

script :

window.onload = initForms;

function initForms() {
for (var i=0; i< document.forms.length; i++) {
    document.forms[i].onsubmit = function() {return validForm();}
}
}

function validForm() {
var allGood = true;
var allTags = document.getElementsByTagName("*");

for (var i=0; i<allTags.length; i++) {
    if (!validTag(allTags[i])) {
        allGood = false;
    }
}
return allGood;

function validTag(thisTag) {
    var outClass = "";
    var allClasses = thisTag.className.split(" ");

    for (var j=0; j<allClasses.length; j++) {
        outClass += validBasedOnClass(allClasses[j]) + " ";
    }

    thisTag.className = outClass;

    if (outClass.indexOf("invalid") > -1) {
        thisTag.focus();
        if (thisTag.nodeName == "INPUT") {
            thisTag.select();
        }
        return false;
    }
    return true;

    function validBasedOnClass(thisClass) {
        var classBack = "";

        switch(thisClass) {
            case "":
            case "invalid":
                break;
            case "reqd":
                if (allGood && thisTag.value == "") {
                    classBack = "invalid ";
                }
                classBack += thisClass;
                break;
            default:
                classBack += thisClass;
        }
        return classBack;
    }
}
}

This code is meant to check if the user entered the required fields in the HTML form.If the user leaves the required field the field is marked with color.

In the above code what does the function validTag(thisTag) is do ? What is the identifier invalid ? This is not declared anywhere in the HTML file.In case here is the HTML file :

<html>
<head>
<title>Car Picker</title>
<script type="text/javascript" src="script.js"></script>    
<link rel="stylesheet" href="script.css" />
</head>
<body>
<h2 align="center">Car Picker</h2>
<form action="#">
<p>
    <label for="emailAddr">Enter your email address:&nbsp;&nbsp;&nbsp;&nbsp;  <input id="emailAddr" type="text" size="30" class="reqd email" />
    </label></br />
    <label for="emailAddr2">Re-enter your email address:<input id="emailAddr2" type="text" size="30" class="reqd emailAddr" />
    </label>
</p>
<p><label for="color">Colors:
    <select id="color" class="reqd">
        <option value="" selected="selected">Choose a color</option>
        <option value="Red">Red</option>
        <option value="Green">Green</option>
        <option value="Blue">Blue</option>
    </select>
</label></p>
<p>Options:
    <label for="sunroof"><input type="checkbox" id="sunroof" value="Yes" />Sunroof (Two door only)</label>
    <label for="pWindows"><input type="checkbox" id="pWindows" value="Yes" />Power Windows</label>
</p>
<p><label for="DoorCt">Doors:&nbsp;&nbsp;
    <input type="radio" id="twoDoor" name="DoorCt" value="twoDoor" class="radio" />Two
    <input type="radio" id="fourDoor" name="DoorCt" value="fourDoor" class="radio" />Four
</label></p>
<p><input type="submit" value="Submit" />&nbsp;<input type="reset" /></p>
    </form>
    </body>
    </html>

The css file :

body {
color: #000;
background-color: #FFF;
}

input.invalid {
background-color: #FF9;
border: 2px red inset;
}

label.invalid {
color: #F00;
font-weight: bold;
}

select {
margin-left: 80px;
}

input {
margin-left: 30px;
}

input+select, input+input {
margin-left: 20px;
}
  • 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-25T15:27:44+00:00Added an answer on May 25, 2026 at 3:27 pm

    If the reqd class is set, the validBasedOnClass function returns the class name invalid if the specified control is empty. In this case, that class is added to the control.
    If the class invalid is added, the control gets the focus too.

    So it’s basically a form validator that ensures that all inputs with the class reqd should be filled before the form is posted.

    But where did you get this code from, if you apparently don’t know what it does? It seems quite unstructured code too. I wouldn’t use it.

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

Sidebar

Related Questions

This is my script : window.onload = function (){ var title = document.getElementsByTagName('h1')[0].id =
How to rewrite the code using jQuery? <script type=text/javascript> window.onload = function(){ var array
When I try the following: <html> <body> <script type=text/javascript> window.onload = function() { var
<html> <head> <title>Sample</title> <script> window.onload = function() { alert(document.getElementById('div1').style.zIndex); alert(document.getElementById('div2').style.zIndex); } </script> <style type=text/css
<script type=text/javascript> window.onload = widthFunction(); window.onresize = function widthFunction(){ var viewportWidth; var fouronefour; rotdiv
<div id=myElement2></div> <script> window.onload = function() { document.getElementById(myElement1).onclick = function() { for (i =
I have put together the following mootools script window.addEvent('domready', function() { var shouts =
My CKEditor code is window.onload = function() { var editor = CKEDITOR.replace( \'big_info\' );
This is a crossbrowser window.onload script, I want to add a message, when the
I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but

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.