Navigate back to the homepage

how to allow special characters in javascript using regex?

Infinitbility
JavaScript
September 18th, 2022 · 1 min read
how to allow special characters in javascript using regex?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To allow special characters, use this regex /^[[email protected]#\$%\^\&*\)\(+=._-]+$/g it will return true even if value contain special characters.

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 special characters 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 special characters in javascript using regex?

Here, I will show allow alphanumeric with special characters in javascript and typescript.

Javascript regex allow special characters example

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

1function validate(param){
2 const regex = /^[[email protected]#\$%\^\&*\)\(+=._-]+$/g;
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 words
15console.log(validate("Infinit"))
16// true
17
18// validate special char
19console.log(validate("[email protected]"))
20// true

Output

Typescript regex allow special characters example

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

1function validate(param: string){
2 const regex = /^[[email protected]#\$%\^\&*\)\(+=._-]+$/g;
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 words
15console.log(validate("Infinit"))
16// true
17
18// validate special char
19console.log(validate("[email protected]"))
20// true

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

how to accept only numbers in javascript using regex?

An example of javascript regex accept only numbers with the step-by-step guide.

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