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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:27:45+00:00 2026-05-18T12:27:45+00:00

I am trying to validate the form input but my validation is not working.

  • 0

I am trying to validate the form input but my validation is not working.
The string must have at least one letter or digit,
an integer should be between 0-11 and should pick at least one fruit.
But even if i dont enter any input,
it just go to the input_ok.html file.
It needs to pop-up the error window.

Here is my code:

function validateInput() {
    var str = myForm.inputString.value;
    var nbrStr = myForm.inputInteger.value;
    var nbr = parseInt(nbrStr);
    var fruit = myForm.fruit.value;
    if (str != "" && nbrStr != "" && fruit != "" && !isNaN(nbr) && nbr < 11) {
        return true;
    } else {
        var msg = "";
        var strError = false;
        if (str == "") {
            msg += "\nPlease enter a string with at least one letter or digit";
            strError = true;
        }
        if (nbrStr == "") {
            msg += "\nPlease enter  an integer in the range 0-11";
        } else if (isNaN(nbr)) {
            msg += "\nPlease enter an integer";
        } else if (nbr > 11) {
            msg += "\nPlease enter an integer less than 11";
        }
        if (strError) {
            myForm.inputString.focus();
        } else {
            myForm.inputInteger.focus();
        }
        alert(msg);
    }
    return false;
}

<body>
    <h1>Week 08, Exercise 01</h1>
    <form action="week08_01servlet" 
          method="post" 
          name="myForm" 
          onsubmit="return validateInput();">
        <p>Please enter the following information, 
        and then click the submit button.</p>
        <p class="indent">A string with at least one letter or digit
            <input type="text" name="inputString">
            <br>An integer in the range 0-11
            <input type="text" name="inputInteger">
            <br>Pick a fruit <select name="fruit">
                <option value="---">---
                <option value="apple">apple
                <option value="banana">banana
                <option value="cherry">cherry
                <option value="pear">pear
            </select></p>
        <p><input type="submit" value="Submit"></p>
        <input type="hidden" name="command" value="do_it">
    </form>
</body>

  • 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-18T12:27:46+00:00Added an answer on May 18, 2026 at 12:27 pm

    This is clearly homework, but I beleave every question deservers an answer. Valid xhtml5, javascript checked with jslint and compressed with ajaxmin. Enjoy.

    Also, you might want to read ppks Introduction to Forms.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>Week 08, Exercise 01</title>
        <link rel="stylesheet" href="Week08_01.css">
        <script>
        function validateInput(c){var f=c.inputString.value,d=c.inputInteger.value,b=parseInt(d,10),g=c.fruit.value;if(f!==""&&d!==""&&g!==""&&!isNaN(b)&&b>=0&&b<11)return true;else{var a="",e=false;if(f===""){a+="\nPlease enter a string with at least one letter or digit";e=true}if(d==="")a+="\nPlease enter  an integer in the range 0-11";else if(isNaN(b))a+="\nPlease enter an integer";else if(b<=0)a+="\nPlease enter a positive integer";else if(b>11)a+="\nPlease enter an integer less than 11";if(e)c.inputString.focus();else c.inputInteger.focus();alert(a);return false}}
        </script>
    </head>
    <body>
        <h1>Week 08, Exercise 01</h1>
        <form action="week08_01servlet" 
            method="post" 
            name="myForm"
            onsubmit="return validateInput(this);">
            <p>Please enter the following information, 
            and then click the submit button.</p>
            <p class="indent">A string with at least one letter or digit
                <input type="text" name="inputString" />
                <br>An integer in the range 0-11
                <input type="text" name="inputInteger" />
                <br />Pick a fruit <select name="fruit">
                    <option value="---">---</option>
                    <option value="apple">apple</option>
                    <option value="banana">banana</option>
                    <option value="cherry">cherry</option>
                    <option value="pear">pear</option>
                </select></p>
            <p><input type="submit" value="Submit">
            <input type="hidden" name="command" value="do_it"></p>
        </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to validate my form fields calling the input text ids, but
I am trying to use jquery validation to validate some input values on the
I am currently trying to validate a form server side, the way it works
I am trying to validate a form in that if there is no text
I have a PHP form that needs some very simple validation on submit. I'd
I am trying to write a simple input field validation plugin at the moment
I am trying to validate user id's matching the example: smith.jack or smith.jack.s In
I'm trying to validate that a submitted URL doesn't already exist in the database.
The XML I'm trying to validate is as follows: <root> <element attribute=foo> <bar/> </element>
We're going through a massive migration project at the minute and trying to validate

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.