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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:12:46+00:00 2026-06-16T02:12:46+00:00

Possible Duplicate: Validate that a string is a positive integer So I want to

  • 0

Possible Duplicate:
Validate that a string is a positive integer

So I want to check a form field if it’s empty and to allow it to has only positive numeric numbers, no spaces or letters. So far I succeed the first part to check if the field is empty with the following code:

function validate(form) {
    var elements = form.elements;

    if (form.qty.value == "") {
        alert("Please enter the field");
        document.forms[0].qty.focus()
        document.forms[0].qty.select()
        return false;
    }

    return true;
}

Somewhere in the form:

<form action="addtocart.php" method ="post" onsubmit="return validate(this);" /> …

But now I want to allow only numbers as well as to check if the field is empty. I have found some scripts but I don’t know how to merge the code into one valid script.

  • 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-16T02:12:48+00:00Added an answer on June 16, 2026 at 2:12 am
    if (Number(form.qty.value) > 0) {
        // Only executes if value is a positive number.
    }
    

    Note that if the value is an empty string the statement will not return true. The same goes for null, NaN (obviously) and undefined. String representations of numbers are transformed into their numeric counterparts.

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

Sidebar

Related Questions

Possible Duplicate: Iphone UITextField only integer I want to place a text field that
Possible Duplicate: How do I validate that a text input contains only latin letters?
Possible Duplicate: Check whether the string is a unix timestamp i need validate a
Possible Duplicate: Validate email address in Javascript? I have an HTML form that uses
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: RegEx to make sure that the string contains at least one lower
Possible Duplicate: Validate numbers in JavaScript - IsNumeric() var miscCharge = $(#miscCharge).val(); I want
Possible Duplicate: Ensure User has entered email address string in correct format? I have
Possible Duplicate: Regular Expression matching for entire string On my form page, I am
Possible Duplicate: How to check if a string is a legal “dd/mm/yyyy” date? I

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.