Navigate back to the homepage

how to allow only numbers and dot in javascript using regex?

Infinitbility
JavaScript
September 14th, 2022 · 1 min read
how to allow only numbers and dot in javascript using regex?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To allow only numbers and dot, use this regex /^[0-9]*\.?[0-9]*$/ it will return true if value contain only numbers and dots.

Let’s see short example to use regex in javascript

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

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 allow only numbers and dot in javascript using regex?

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

Javascript regex allow only numbers and dot example

Here, we will create common validate function where we will validate param should numbers and dots.

1function validate(param){
2 const regex = /^[0-9]*\.?[0-9]*$/;
3 return regex.test(param);
4}
5
6// validate integer
7console.log(validate(39))
8
9// validate float
10console.log(validate(39.89))
11
12// validate string float
13console.log(validate("39.89"))
14
15// validate string float with e
16console.log(validate("39.89e"))
17
18// validate char string
19console.log(validate("one"))
20
21// validate Boolean
22console.log(validate(true))

Output

Typescript regex allow only numbers and dot example

Same like javascript, we will create a function and call it with diffrent parameters.

1function validate(param: number){
2 const regex = /^[0-9]*\.?[0-9]*$/;
3 return regex.test(param.toString());
4}
5
6// validate integer
7console.log(validate(39))
8
9// validate float
10console.log(validate(39.89))

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 check current typescript version?

how to check current typescript version?

An example of check current typescript version with the step-by-step guide.

September 13th, 2022 · 1 min read
how to check data type in react native?

how to check data type in react native?

An example of check data type in react native with the step-by-step guide.

September 8th, 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]