0

I would like to check few of my text boxes that must satisfy the following conditions: Alphabets i meant are from a-z(uppercase and lower case) numbers 0-9 and special characters are ~`!@#$%^&*()-_+={}[];:'",.<>/?

  1. It can contain only alphabets
  2. It cannot contain only numbers
  3. It cannot contain only special characters
  4. It cannot contain only numbers and special characters
  5. It can contain alphabets,numbers and special characters
  6. It can contain alphabets and numbers
  7. It can contain alphabets and special charcters

I found a solution but seems not working for me:

/^[a-z0-9/. -!@#$%^&*(){}:;"',/?]+$/i 

I am checking it as:

var alpha=/^[a-z0-9/. -!@#$%^&*(){}:;"',/?]+$/i; if (!alpha.test(username.value)) { alert('Invalid username'); document.theForm.username.focus(); return false; } 
5
  • Need clarification: 1. What is a "special character"? 2. You tagged this Java AND JavaScript, but Java regexes are much more convenient since they can use \p{L} to pick up any Unicode letter; to do that in JavaScript is beyond tedious. What do you mean by "alphabets"? Commented Jun 20, 2013 at 5:44
  • 1
    To simplify 1 - 7: It must contain at least one letter, and may also contain digits and special characters. Commented Jun 20, 2013 at 5:47
  • Not exactly a duplicate, but close enough: Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number) - you can easily adapt this solution to your problem. Commented Jun 20, 2013 at 5:51
  • i have edited my question can you please review it Commented Jun 20, 2013 at 5:52
  • If it contains non-special character like back slash ``, is it invalid? Commented Jun 20, 2013 at 5:52

2 Answers 2

2

The problem can be restated as that of matching a string containing ONLY the characters

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

such that at least one of them is a letter.

Fortunately, you've covered all the printable characters in the range U+0021 to U+007F, so that the desired regex is simply

[!-~]*[A-Za-z][!-~]* 

EDIT: On closer reading, I noticed you did not allow the backslash! If you want to allow the backslash, the regex above is okay; if not, you should modify it like so:

[!-\[\]-~]*[A-Za-z][!-\[\]-~]* 

It's a bit uglier, because to exclude the backslash we have to say

  • All characters in the range ! to [ union characters in the range ] to ~, and the explicit mention of [ and ] requires escaping with, you guessed it, the \.

Hopefully you meant to allow the \ so you can use the simpler regex above.

EDIT 2

To make the regex more efficient, you should use a reluctant quantifier (as kcsoft did):

[!-~]*?[A-Za-z][!-~]* 

Also for JavaScript, but not for Java if you are using matches, you should anchor the regex to match the whole string, giving this in JavaScript:

/^[!-~]*?[A-Za-z][!-~]*$/ 

And, as you did in your question, you can shorten it a bit more by using the i modifier:

/^[!-~]*?[A-Z][!-~]*$/i 
Sign up to request clarification or add additional context in comments.

2 Comments

I doubt the OP knows what "special" characters he/she really needs. For example [, ], < and > were missing from the regex, but later included in the description. Also, / is included twice, so maybe one of them should be \\ .
Yeah, I'm guessing the intent was "all ASCII printable punctuation," that is, characters in the range U+0021 to U+007F that aren't letters and numbers.
0

Can you give some input examples. Can you try this?

/.*?[a-zA-Z]+.*/ 

Or if you need to specify the list of special characters:

/[list of chars]*?[a-zA-Z]+[list of chars]*/ 

3 Comments

@user2322631 This regex allows characters outside of your list, for example é and ü and spaces and tabs and all kinds of control characters, because the . is allowed to match any character except a new line. Be careful with dots in regexes.
if i awnted to include only special character like -:, what changes should i make in the above regex
You would need to define your list in the regex. Like /[list of chars]*?[a-zA-Z]+[list of chars]*/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.