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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:13:56+00:00 2026-06-05T08:13:56+00:00

function validInteger(theNumber){ var anyNonDigits = new RegExp(‘\D’,’g’); if(parseInt(theNumber)&&!anyNonDigits.test(theNumber)){ return true; }else{ return false; }

  • 0
function validInteger(theNumber){
    var anyNonDigits = new  RegExp('\D','g');
    if(parseInt(theNumber)&&!anyNonDigits.test(theNumber)){
        return true;
    }else{
        return false;
    }
}

Above is a function I’ve written to validate some input. I want all positive integers. The problem I’m facing is with the RegExp object. This seems like it should be super simple, but for some reason it’s not working.

For example if I pass ‘f5’ I get true, but if I pass ‘5f’ I get false. I’m also having problems when passing negative numbers. -3 doesn’t get caught even if I stringify the variable before passing it into the RegExp. I can fix this by adding ‘&&parseInt(theNumber)>0‘ in my if statement, but I feel like the RegExp should catch that too. Thanks in advance!

  • 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-05T08:13:57+00:00Added an answer on June 5, 2026 at 8:13 am

    Simply:

    function validInteger(theNumber){    
        return theNumber.match(/^\d+$/) && parseInt(theNumber) > 0;
    }
    

    Live DEMO

    Or even simpler with regex only as suggested by @Eric:

    return /^[0-9]\d*$/.test(theNumber);
    

    Live DEMO

    Update:

    An excellent cheat sheet. The link died after 5 years, sorry.

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

Sidebar

Related Questions

function countChars(elm) { if (elm.nodeType == 3) { // TEXT_NODE return elm.nodeValue.length; } var
function example(){ var quantity = http://www.example.com/index.php?brandid=b%123&quantity=20; quantity = quantity.replace(-what regular expression should i user-,
function controllaFoto(target, username) { var _URL = window.URL || window.webkitURL; var img = new
function test(){ $embedmode = 'normal'; if ( ( $embedmode != '' ) && (
function initialize() { var myOptions = { center: new google.maps.LatLng(23.71181,90.407467), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP
function MyFunc: Boolean; begin if eval then Result := True else Result := False;
function test() { var flag1 = false; if ($('#field1').attr('value') || !$('#field1').attr('value')) { $.ajaxSetup({ cache:
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
Function FillAdminAccount() As Boolean FillAdminAccount = True Try SQLconn.ConnectionString = connect timeout=9999999; & _
function getLink($sa){ if($sa=='1'){ $sa = 'qp_bbl'; } else { $sa = 'qp_sbl'; } return

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.