Navigate back to the homepage

how to accept only numbers in javascript using regex?

Infinitbility
JavaScript
September 17th, 2022 · 1 min read
how to accept only numbers in javascript using regex?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To accept only numbers, use this regex ^[0-9]*$ it will return true if value contain only numbers.

Let’s see short example to use regex in javascript

1const regex = /^[0-9]*$/;
2console.log(regex.test(90))

Today, I’m going to show you How do I check value contain only numbers and dot in javascript, as above mentioned, I’m going to use the above-mentioned regex with test() method.

Let’s start today’s tutorial how do you accept only numbers in javascript using regex?

Here, I will show accept only numbers in javascript and typescript.

Javascript regex accept only numbers example

Here, we will create common validate function where we will validate param should contain only numbers.

1function validate(param){
2 const regex = /^[0-9]*$/;
3 return regex.test(param);
4}
5
6// validate empty
7console.log(validate(""))
8// false
9
10// validate number
11console.log(validate(39))
12// true
13
14// validate zero
15console.log(validate(0))
16// true
17
18// validate nan
19console.log(validate(NaN))
20// false

Output

Typescript regex accept only numbers example

Here, we will create common validate function where we will validate param should contain only numbers.

1function validate(param: number){
2 const regex = /^[0-9]*$/;
3 return regex.test(param.toString());
4}
5
6
7// validate number
8console.log(validate(39))
9// true
10
11// validate zero
12console.log(validate(0))
13// true
14
15// validate nan
16console.log(validate(NaN))
17// false

Output

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to allow space between words in javascript using regex?

how to allow space between words in javascript using regex?

An example of javascript regex allow space between words with the step-by-step guide.

September 16th, 2022 · 1 min read
how to allow only alphanumeric and space in javascript using regex?

how to allow only alphanumeric and space in javascript using regex?

An example of javascript regex allow only alphanumeric and space with the step-by-step guide.

September 15th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]