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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:13:30+00:00 2026-05-17T19:13:30+00:00

I am trying to get this Javascript in my application working. function validateQuantity(field) {

  • 0

I am trying to get this Javascript in my application working.

function validateQuantity(field)
{
    var value = field.value; //get characters
    //check that all characters are digits, ., -, or ""
    for(var i=0;  i < field.value.length; ++i)
    {
        var new_key = value.charAt(i); //cycle through characters
        if(((new_key <= "0") || (new_key > "9")) &&
            !(new_key == ""))
        {
            alert("Please enter number and greater than 0 only");
            return false;
            break;
        }
        return true;
    }
}

And I have a input button as below

<input class="buttonToLink" type="submit" value="Update" 
       onclick="return validateQuantity(document.getElementById('quantity'))"/>

The above code successfully checks the input of all alphabet such as “abc” or alphabet and numeric such as “abcd123” as false.

However, when I put numeric characters first, along with alphabet such as “123abc”, it fails — it does not show the alert.

What did I do wrong with the code, and how can it be fixed?

  • 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-17T19:13:30+00:00Added an answer on May 17, 2026 at 7:13 pm
    function validateQuantity(field) {
        if (!/^\d+$/.test(field.value)) { // is an integer
           alert("Please enter number and greater than 0 only");
           return false;
        }
    
        return true;
    }
    

    The reason your code doesn’t work is because you have the return true statement inside the loop. As soon as it sees a valid integer it will return true and break out of the function, ignoring anything that comes after it. Allowing strings like “123abc” for example.

    This is probably what you wanted:

    function validateQuantity(field)
    {
        var value = field.value; //get characters
        //check that all characters are digits, ., -, or ""
        for(var i=0;  i < field.value.length; ++i)
        {
            var new_key = value.charAt(i); //cycle through characters
            if(((new_key <= "0") || (new_key > "9")) &&
                !(new_key == ""))
            {
                alert("Please enter number and greater than 0 only");
                return false;
                break;
            }
        }
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
I'm trying to get this simple PowerShell script working, but I think something is
I'm trying to get this sample for AJAX to WCF working, with the following
I'm trying to get this piece of code working a little better. I suspect
I've been trying to get this code to work for hours! All I need
I get this message when trying to communicate with software that communicates with SOAP?
hI, I'm trying to get this code from Larry Nyhoff's book to compile in
I have been trying to get around this error for a day now and
I get this error: System.Reflection.TargetException: Object does not match target type. when trying to
I am trying to deserialize a stream but I always get this error End

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.