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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:00:10+00:00 2026-05-14T05:00:10+00:00

Problem statement: It is necessary for me to write a code, whether which before

  • 0

Problem statement:
It is necessary for me to write a code, whether which before form sending will check all necessary fields are filled. If not all fields are filled, it is necessary to allocate with their red colour and not to send the form.

Now the code exists in such kind:

function formsubmit(formName, reqFieldArr){     
    var curForm = new formObj(formName, reqFieldArr);
    if(curForm.valid)
        curForm.send();
    else
        curForm.paint();    
}


function formObj(formName, reqFieldArr){
    var filledCount = 0;
    var fieldArr = new Array();
    for(i=reqFieldArr.length-1; i>=0; i--){
        fieldArr[i] = new fieldObj(formName, reqFieldArr[i]);
        if(fieldArr[i].filled == true)
        filledCount++;
    }
    
    if(filledCount == fieldArr.length)
        this.valid = true;
    else
        this.valid = false;
    
    
    this.paint = function(){
        for(i=fieldArr.length-1; i>=0; i--){
            if(fieldArr[i].filled == false)
                fieldArr[i].paintInRed();
            else
                fieldArr[i].unPaintInRed();
        }
    }
    
    this.send = function(){
        document.forms[formName].submit();
    }
}


function fieldObj(formName, fName){
    var curField = document.forms[formName].elements[fName];
    
    if(curField.value != '')
        this.filled = true;
    else
        this.filled = false;
        
    this.paintInRed = function(){
        curField.addClassName('red');
    }
    
    this.unPaintInRed = function(){
        curField.removeClassName('red');
    }
}

Function is caused in such a way:

<input type="button" onClick="formsubmit('orderform', ['name', 'post', 'payer', 'recipient', 'good'])"  value="send" />

Now the code works. But I would like to add "dynamism" in it.

That it is necessary for me: to keep an initial code essentially, to add listening form fields (only necessary for filling).

For example, when the field is allocated by red colour and the user starts it to fill, it should become white.

As a matter of fact I need to add listening of events: onChange, blur for the blank fields of the form. As it to make within the limits of an initial code.

If all my code – full nonsense, let me know about it. As to me it to change using object-oriented the approach.


Give me pure Javascript solution, please. Jquery – great lib, but it does not approach for me.

  • 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-14T05:00:11+00:00Added an answer on May 14, 2026 at 5:00 am

    Has made.

    function formsubmit(formName, reqFieldArr){     
        var curForm = new formObj(formName, reqFieldArr);
        if(curForm.valid)
            curForm.send();
        else{
            curForm.paint();    
            curForm.listen();
        }
    }
    
    
    function formObj(formName, reqFieldArr){
        var filledCount = 0;
        var fieldArr = new Array();
        for(i=reqFieldArr.length-1; i>=0; i--){
            fieldArr[i] = new fieldObj(formName, reqFieldArr[i]);
            if(fieldArr[i].filled == true)
            filledCount++;
        }
    
        if(filledCount == fieldArr.length)
            this.valid = true;
        else
            this.valid = false;
    
    
        this.paint = function(){
            for(i=fieldArr.length-1; i>=0; i--){
                if(fieldArr[i].filled == false)
                    fieldArr[i].paintInRed();
                else
                    fieldArr[i].unPaintInRed();
            }
        }
    
        this.send = function(){
            document.forms[formName].submit();
        }
    
        this.listen = function(){
            for(i=fieldArr.length-1; i>=0; i--){
                fieldArr[i].fieldListen();
            }
        }
    }
    
    function fieldObj(formName, fName){
        var curField = document.forms[formName].elements[fName];
    
        this.filled = getValueBool();
    
        this.paintInRed = function(){
            curField.addClassName('red');
        }
    
        this.unPaintInRed = function(){
            curField.removeClassName('red');
        }
    
        this.fieldListen = function(){
            curField.onkeyup = function(){
                if(curField.value != ''){ 
                    curField.removeClassName('red');
                }
                else{
                    curField.addClassName('red');
                }
            }
        }
    
        function getValueBool(){
            if(curField.value != '')
                return true;
            else
                return false;
        }
    }
    

    This code is not pleasant to me, but it works also I could not improve it without rewriting completely.

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

Sidebar

Related Questions

Problem statement: Find the right triangle that has integers for all sides and all
Problem Statement is : Given 2 Dimensional array, print output for example If 4
Problem statement We have one employer that wants to interview N people, and therefore
Problem Statement: I'm creating a template for multi tiered complicated calculations in MS Excel
Here is the problem statement: Calling a setter on the object should result in
I'm having some problem with this statement declare @result int select @result = (select
I have a problem with a continue statement in my C# Foreach loop. I
I have a problem with the SQL statement detailed below. The query returns the
I have an big problem with an SQL Statement in Oracle. I want to
Problem statement: A table contains an item_id, a category_id and a date range (begin_date

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.