Match against /^\d+$/. $ means "end of line", so any non-digit characters after the initial run of digits will cause the match to fail.

Edit:

RobG wisely suggests the more succinct /\D/.test(z). This operation tests the inverse of what you want. It returns true if the input has any non-numeric characters.

Simply omit the negating ! and use if(/\D/.test(z)).

Answer from apsillers on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › number-validation-in-javascript
Number Validation in JavaScript - GeeksforGeeks
July 15, 2025 - Here are the various methods to validate numbers in JavaScript · Use typeof and isNaN() to determine if a value is a valid number. JavaScript · const isNum = n => typeof n === 'number' && !isNaN(n); console.log(isNum(42)); console.log(isN...
🌐
Numverify
numverify.com › number-validation-in-javascript
JavaScript Numerical Data Validation: Techniques & Best Practices
Learn how to verify numerical data in JavaScript effectively. Explore techniques like isNaN, typeof, regex, parseInt, Number.isFinite & custom functions for error-free applications.
Discussions

Javascript - validation, numbers only - Stack Overflow
I'm trying to get my login form to only validate if only numbers were inputted. I can it to work if the input is only digits, but when i type any characters after a number, it will still validate e... More on stackoverflow.com
🌐 stackoverflow.com
Build a Telephone Number Validator
Hey so I finished writing my JS for the Telephone Validator project. Something weird happens and I can’t work out what’s going on… So for a few of the numbers the validation function returns true, but then I run it again and it returns false, then true and back and forth. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
February 23, 2024
Validate decimal numbers using JavaScript - IsNumeric() function
What is the most straightforward and efficient method for validating decimal numbers in JavaScript? Extra considerations for: Clarity. The solution should be simple and tidy. Compatibility across different platforms. Test scenarios: 01. IsNumeric('-1') => true 02. More on community.latenode.com
🌐 community.latenode.com
0
0
October 3, 2024
Real-time serial number validation with JavaScript

That doesn’t sound very difficult to do in real time

More on reddit.com
🌐 r/programming
1
0
August 23, 2015
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › input › number
<input type="number"> - HTML | MDN
Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement value property. Note: Because a read-only field cannot have a value, required does not have any effect on inputs with the readonly attribute also specified. The step attribute is a number that specifies the granularity that the value must adhere to, or the special value any, which is described below. Only values which are a whole number of steps from the step base are valid...
🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Validation
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
Kevinleary
kevinleary.net › blog › validating-real-phone-numbers-javascript
Kevinleary.net: JavaScript Phone Number Validation: Regex & libphonenumber-js
December 8, 2024 - Validate phone numbers in JavaScript using regular expressions and the libphonenumber-js library. This guide covers simple regex, NANP validation, and international numbers.
🌐
AbstractAPI
abstractapi.com › api guides, tips & tricks › javascript phone number validation
JavaScript Phone Number Validation | Abstract API
1 week ago - In this article, we’ll walk you through how to validate phone numbers in JavaScript, using regex, popular libraries, and validation APIs.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-validate-number-string-in-javascript
How to Validate Number String in JavaScript ? - GeeksforGeeks
August 5, 2025 - We will use various methods to validate number strings in JavaScript like the isNaN Function, the Number() Function, and the isFinite Function.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Build a Telephone Number Validator - JavaScript - The freeCodeCamp Forum
February 23, 2024 - Hey so I finished writing my JS for the Telephone Validator project. Something weird happens and I can’t work out what’s going on… So for a few of the numbers the validation function returns true, but then I run it aga…
🌐
W3Resource
w3resource.com › javascript › form › all-numbers.php
JavaScript: HTML Form validation - checking for all numbers
August 19, 2022 - To get a string contains only numbers (0-9) with a optional + or - sign (e.g. +1223, -4567, 1223, 4567) we use the regular expression (/^[-+]?[0-9]+$/). Next, the match() method of the string object is used to match the said regular expression ...
🌐
DEV Community
dev.to › rthefounding › checking-if-telephone-number-is-valid-4iop
Validating Phone Numbers with JavaScript - DEV Community
September 2, 2021 - Here they want us to return true if the passed string looks like a valid US phone number. The person... Tagged with javascript, tutorial, beginners, devops.
🌐
CoreUI
coreui.io › blog › how-to-check-if-string-is-number-in-javascript
How to check if a string is a number in JavaScript · CoreUI
February 11, 2024 - JavaScript’s Number() function is a handy tool for converting strings to numbers. It’s straightforward: if the string represents a valid number, Number() converts and returns it.
🌐
npm
npmjs.com › package › libphonenumber-js
libphonenumber-js - npm
2 weeks ago - First checks the phone number length and then checks the phone number digits against all available regular expressions. By default, this library uses min ("minimal") metadata which is only 80 kB in size but also doesn't include the precise validation regular expressions resulting in less strict validation rules (some very basic validation like number length check is still included for each country).
      » npm install libphonenumber-js
    
Published   Mar 13, 2026
Version   1.12.40
Author   catamphetamine
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › math › validate number
Validate a number in JavaScript - 30 seconds of code
May 17, 2024 - const validateNumber = n => { const num = parseFloat(n); return !Number.isNaN(num) && Number.isFinite(num) && Number(n) == n; } validateNumber('10'); // true validateNumber('a'); // false
🌐
W3Resource
w3resource.com › javascript › form › phone-no-validation.php
JavaScript : phone number validation - w3resource
August 19, 2022 - Now, let's see how to validate a phone number with country code, either in +24-0455-9034, +21.3789.4512 or +23 1256 4587 format. ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript form validation - checking non-empty</title> <link rel='stylesheet' href='form-style.css' type='text/css' /> </head> <body onload='document.form1.text1.focus()'> <div class="mail"> <h2>Input an Phone No.[+xx-xxxx-xxxx, +xx.xxxx.xxxx, +xx xxxx xxxx] and Submit</h2> <form name="form1" action="#"> <ul> <li><input type='text' name='text1'/></li> <li>&nbsp;</li> <li class="submit"><input type="submit" name="submit" value="Submit" onclick="phonenumber(document.form1.text1)"/></li> <li>&nbsp;</li> </ul> </form> </div> <script src="phoneno-+international-format.js"></script> </body> </html>
🌐
IBM
ibm.com › docs › en › radfws › 9.7.0
Number validator
We cannot provide a description for this page right now
🌐
W3Schools
w3schools.com › js › js_validation.asp
JavaScript Form Validation
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... HTML form validation can be done by JavaScript.
🌐
Latenode
community.latenode.com › other questions › javascript
Validate decimal numbers using JavaScript - IsNumeric() function - JavaScript - Latenode Official Community
October 3, 2024 - What is the most straightforward ... be simple and tidy. Compatibility across different platforms. Test scenarios: 01. IsNumeric('-1') => true 02....