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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:25:54+00:00 2026-06-04T15:25:54+00:00

I need help with simplifying/making shorter of the following validation code. Help would be

  • 0

I need help with simplifying/making shorter of the following validation code.
Help would be greatly appreciated.
At the moment there’s too much text, my teacher said it could be done easier/cleaner…
I’m really stuck.

Thank you.

window.addEventListener('load',init,false);

function init(){
    var submit = document.getElementById("submit");
    var gender = document.getElementById("gender");
    var age = document.getElementById("age");
    var length = document.getElementById("length");
    var weight = document.getElementById("weight");
    var duration = document.getElementById("duration");
    var time = document.getElementById("time");

    submit.addEventListener('click', validation, false);
    gender.addEventListener('checked',validategender,false);
    age.addEventListener('blur', validateage, false);
    length.addEventListener('blur',validatelength,false);
    weight.addEventListener('blur',validateweight,false);
    duration.addEventListener('blur',validateduration,false);
    time.addEventListener('checked',validatetime,false);
}

function validategender(){
    var man = document.getElementById("man");
    var vrouw = document.getElementById("vrouw");
    var genderfout = document.getElementById("genderFout");
    if(man.checked != true && vrouw.checked !=true){
        genderfout.innerHTML = "Please choose a gender";
        return false;
    }else {
        genderfout.innerHTML = "";
    }return true;
}

function validateage() {
    var age = parseInt(document.getElementById("age").value, 10);
    var ageFout = document.getElementById("ageFout");
    if (isNaN(age) || age < 0 || age > 130) {
        ageFout.innerHTML = "Please enter a valid age";
        return false;
    } else {
        ageFout.innerHTML = "";
    }
    return true;
}

function validateweight() {
    var weight = parseInt(document.getElementById("weight").value, 10);
    var weightFout = document.getElementById("weightFout");
    if (isNaN(weight) || weight < 30 || weight > 200) {
        weightFout.innerHTML = "Please enter a valid weight";
        return false;
    } else {
        weightFout.innerHTML = "";
    }
    return true;
}

function validatelength() {
    var length = parseInt(document.getElementById("length").value, 10);
    var lengthFout = document.getElementById("lengthFout");
    if (isNaN(length) || length < 50 || length > 220) {
        lengthFout.innerHTML = "Please enter a valid length";
        return false;
    } else {
        lengthFout.innerHTML = "";
    }
    return true;
}

function validateduration() {
    var duration = parseInt(document.getElementById("duration").value, 10);
    var durationFout = document.getElementById("durationFout");
    if (isNaN(duration) || duration < 0) {
        durationFout.innerHTML = "Please enter a valid time-duration";
        return false;
    } else {
        durationFout.innerHTML = "";
    }
    return true;
}

function validatetime(){
    var minutes = document.getElementById("minutes");
    var hours = document.getElementById("hours");
    var timeFout = document.getElementById("timeFout");
    if(minuten.checked !=true && uur.checked !=true){
        timeFout.innerHTML = "choose a time unit!";
        return false;
    }else {
        timeFout.innerHTML = "";
    }
    return true;
}

function validation(e){
    var genderOk = validategender();
    var ageOk = validateage();
    var weightOk = validateweight();
    var lengthOk = validatelength();
    var durationOk = validateduration();
    var timeOk = validatetime();

    if (!genderOk || !ageOk || !weightOk || !lengthOk ||  !durationOk || !timeOk){
        e.preventDefault();
    }
}
  • 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-04T15:25:56+00:00Added an answer on June 4, 2026 at 3:25 pm

    if JQuery not allowed that leave another option

    Regular expression

    you can validate but needs a little reading

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

Sidebar

Related Questions

Need help with deleting call log from multi numbers, using the following code i
Need help on this following code. the problem is it only trigger the last
Need help to figure out why the following code for basic Binary Search Tree
I need help doing the following: a preprocessor macro label(x) shall output #x, e.g.,
Need help. I have Graph API code that uses an auth token with offline
Need help to convert code from asp control to input type to fetch file
I need help in simplifying the below query. I was able to check for
Need help with following situation: Users can generate their own data structures which are
Need help with the following. I am creating two different combinations of view/controllers that
Need help parsing... In my code, I have a method that returns url.getHost();. 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.