Navigate back to the homepage

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

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

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To allow only alphanumeric and space, use this regex /^[a-z\d\s]+$/i it will return true if value contain only alphanumeric and spaces.

Let’s see short example to use regex in javascript

1const regex = /^[a-z\d\s]+$/i;
2console.log(regex.test("hello 8943"))

Today, I’m going to show you How do I check value contain only alphanumeric and space 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 alphanumeric and space in javascript using regex?

If you want to also allow underscore and hyphen use this regex /^[a-z\d\-_\s]+$/i.

Here, I will show allow only alphanumeric and space in javascript and typescript.

Javascript regex allow only alphanumeric and space example

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

1const regex = /^[a-z\d\s]+$/i;
2
3// Truthy values
4// word string
5console.log(regex.test("INFINIT"))
6
7// word with space
8console.log(regex.test("INF INIT"))
9
10// word with number
11console.log(regex.test("INFINIT 374"))
12
13// number with space
14console.log(regex.test("3546 374"))
15
16// Falsy values
17// empty string
18console.log(regex.test(""))
19
20// hyphen
21console.log(regex.test("INF-INIT"))
22
23// plus
24console.log(regex.test("INFINIT+374"))
25
26// underscore
27console.log(regex.test("3546_374"))

Output

Typescript regex allow only alphanumeric and space example

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

1const regex = /^[a-z\d\s]+$/i;
2
3// Truthy values
4// word string
5console.log(regex.test("INFINIT"))
6
7// word with space
8console.log(regex.test("INF INIT"))
9
10// word with number
11console.log(regex.test("INFINIT 374"))
12
13// number with space
14console.log(regex.test("3546 374"))
15
16// Falsy values
17// empty string
18console.log(regex.test(""))
19
20// hyphen
21console.log(regex.test("INF-INIT"))
22
23// plus
24console.log(regex.test("INFINIT+374"))
25
26// underscore
27console.log(regex.test("3546_374"))

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

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

An example of javascript regex allow only numbers and dot with the step-by-step guide.

September 14th, 2022 · 1 min read
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
© 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]