0

For text validation for chars I am using like [a-zA-z] and for numbers like [0-9] ..if I need to add special symbols I am adding slash like [a-zA-z/-/].

While including lot of symbols its getting difficult and my javascript is getting extremely big. Is there an easy way to do it ?

Regards A.Collins

1
  • 3
    Note that [A-z] is equivalent to [A-Z[\\\\]^_`a-z] so it does not only contain AZ but also az and the characters [, \‍, ], ^, _ and ` that are between Z and a. Commented Dec 5, 2010 at 10:52

5 Answers 5

1

You can take a look at this cheat sheet. for instance, [0-9] can be reduced to \d.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks npinti. Its very helpful
0

For the general case of "a lot of characters" — no.

Comments

0
  • \w for alphanumerics and underscores
  • \d for digits
  • \s for whitespace

You can mix them, resulting in stuff like, for example, [\d.] (for matching numbers & dots).

Comments

0

In a character class, x-y means "all characters between x and y". If you just have one additional character, in your case / then you don't need to use the x-y format, you can just drop the character in:

[a-zA-z/]

Comments

0

That's not the correct way to escape characters. \ is the correct escape character to be used:

[a-zA-Z\/]

You can use character classes, [a-zA-Z0-9] can be replaced by [\w]. The only characters that needs to be escaped are \ and -. ^ should be escaped too when it's the first character in the character class.

2 Comments

He's adding a slash to his character class, not a hyphen.
In this case there are two reasons why the hyphen does not need to be escaped. It can not be interpreted as part of a character range because (1) the previous character already is part of a character range; (2) it is at the end of the character class.

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.