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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:56:19+00:00 2026-06-06T03:56:19+00:00

i need a javascript function that able to check for digit and – only.

  • 0

i need a javascript function that able to check for digit and – only.

example: 1,2,3,4,5,6,7,8,9,0 will return true
and – will return true as well.

other than that all return false including enter is pressed.

i have a function like this:

function IsNumeric(sText){
    var filter = /^[0-9-+]+$/;
    if (filter.test(sText)) {
        return true;
    }else {
        return false;
    }
}

i call it like this:

if(!IsNumeric(value)) {
  alert("Number and - only please");
}

for some reason it does not work, any method to do the verification without using regex?

  • 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-06T03:56:21+00:00Added an answer on June 6, 2026 at 3:56 am

    EDIT: OK, updated as per your comment, an expression to match either a lone minus sign or any combination of digits with no minus sign:

    function IsNumeric(sText){
       return /^(-|\d+)$/.test(sText);
    }
    

    If you want only positive numbers and don’t want to allow leading zeros then use this regex:

    /^(-|[1-9]\d*)$/
    

    Regarding your question “any method to do the verification without using regex?”, yes, there are endless ways to achieve this with the various string and number manipulation functions provided by JS. But a regex is simplest.

    Your function returns true if the supplied value contains any combination of digits and the plus or minus symbols, including repeats such as in "---+++123". Note that the + towards the end of your regex means to match the preceding character 1 or more times.

    What you probably want is a regex that allows a single plus or minus symbol at the beginning, followed by any combination of digits:

    function IsNumeric(sText){
       return /^[-+]?\d+$/.test(sText);
    }
    

    ? means match the preceding character 0 or 1 times. You can simplify [0-9] as \d. Note that you don’t need the if statement: just return the result from .test() directly.

    That will accept “-123”, “123”, “+123” but not “–123”. If you don’t want to allow a plus sign at the beginning change the regex to /^-?\d+$/.

    “example: 1,2,3,4,5,6,7,8,9,0 will return true and – will return true as well.”

    Your example seems to be saying that only a single digit or a single minus sign is considered valid – if so then try this:

    function IsNumeric(sText){
       return /^[\d-]$/.test(sText);
    }
    

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

Sidebar

Related Questions

I need to create a javascript function that checks if it has been a
I need to write, in JavaScript (I am using jQuery), a function that is
I have a simple javascript function that takes two variables. I need to pass
I need a javascript function f that given another (anonymous) function g and a
I have a JavaScript function that handles mouse button events. It must be able
I need JavaScript working almost perfectly in my application that should be able to
I need a java script function that converts the document object of the current
I need 2 simple JavaScript functions that read and change the color of a
I need to pass a variable to a javascript function,but I got a little
I need to pass an url from asp.net load_page to flowplayer javascript function here:

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.