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

const regex = /^[0-9]*\.?[0-9]*$/;
console.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.

function validate(param){
  const regex = /^[[email protected]#\$%\^\&*\)\(+=._-]+$/g;
  return regex.test(param);
}

// validate empty
console.log(validate(""))
// false

// validate number
console.log(validate("39"))
// true

// validate words
console.log(validate("Infinit"))
// true

// validate special char
console.log(validate("[email protected]"))
// true

Output

Typescript regex allow special characters example

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

function validate(param: string){
  const regex = /^[[email protected]#\$%\^\&*\)\(+=._-]+$/g;
  return regex.test(param);
}

// validate empty
console.log(validate(""))
// false

// validate number
console.log(validate("39"))
// true

// validate words
console.log(validate("Infinit"))
// true

// validate special char
console.log(validate("[email protected]"))
// true

Output

I hope it helps you, All the best 👍.