0

I want to implement regular express for below condition , can any one help please

Numeric 0-9 with special character/ \ @ # $ % ^ & * ! - ~ . _ ( ) & space allowed


like : 123abc not allowed
like : 123#$%~. are allowed

3 Answers 3

5

You've pretty much wrote the regular expression yourself, you need to add those characters to a character class with proper escaping, use a quantifier and anchor your expression.

^[0-9/\\@#$%^&*!~._() -]+$

In C#, you can use the Regex.IsMatch() method to validate:

if (Regex.IsMatch(input, @"^[0-9/\\@#$%^&*!~._() -]+$")) { ... }

In JS, you can use the test() method to validate:

var re = /^[0-9/\\@#$%^&*!~._() -]+$/
if (re.test(input)) { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this,

^[0-9!@#$%^&~*()_+\-=\[\]{};':"\\|,.<>\/?]*$

Here is working example

Comments

1

For the regex itself, you should be able to pretty much list the allowed characters in square brackets:

^[0-9/\\@#$%^&*!-~._()\ ]*$

Use * at the end (before the trailing $) to match 0 or more characters (i.e. if empty string is OK), or use + at the end (before the trailing $) to match 1 or more characters.

Depending on the language / regex implementation, you might need to escape more of those characters within the pattern string.

2 Comments

Thanks Paul for quick reply. it's taking alphabets value also like a,b,c,d....I want to disabled alphabets.
OK, so you want to match the full input. I've edited my answer above to include ^ at the beginning and $ at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.