views:

49

answers:

3

Hi, I am trying to validate an input string (which can be copied & pasted in the textbox). The input entered should contain caps alphabates (one or more than one), numbers and two more special characters as (& and Ñ). Also there should not be any spaces in between.

NB: Alphabates must present, but numbers & those two special characters may or may not present to create a valid input.

I am using Jquery for client side validation.

  • Alternative Approach:

    If anyone can help me how we can detect the occurance of any special characters and spaces, except '&' and 'Ñ' in any input string, then also my problem can be resolved.

A: 

If you only want to allow capital letters, digits, and & or Ñ as valid characters, then use

^(?=.*[A-Z])[A-Z\d&Ñ]*$

Verbose regex:

^                   # anchor the search at the start of the string
(?=.*[A-Z])         # assert that there is at least one letter A-Z in the string
[A-Z\d&Ñ]*       # match any number of allowed characters
$                   # anchor the search at the end of the string

You might want to impose a minimum length, though, as currently the string A would be a valid input. For example, ^(?=.*[A-Z])[A-Z\d&Ñ]{8,}$ would ensure a minimum length of 8.

If you want a regex that will match if the input contains a non-allowed character, then you can use

[^A-Z\d&Ñ]

But then you need a second regex to also ensure that there is at least one letter in your input. I'd argue the first approach is more readable, even in JavaScript where you can't use verbose/commented regexes.

Tim Pietzcker
Biki
No more complicated than your rules :)
Tim Pietzcker
Biki
How is this less complicated and also ensures that there is at least one letter?
Tim Pietzcker
Actually I am done with all validation with one regex exp
Biki
I don't quite see the point of that. What happens if your user cuts (Ctrl-X) a section of the input so that the only letter he entered is gone? As I understand it, you'll have to do a complete validation of the final string. But I don't know JQuery. Anyway, I have edited my answer and added a "illegal character detection regex".
Tim Pietzcker
Biki
OK, I have again edited my answer. You too should edit your question and specify that you only want to allow capital letters, otherwise my answer doesn't fit the question as it is currently stated.
Tim Pietzcker
Done, Thanks Tim.
Biki
A: 

Detecting what is not allowed in this situation would not be a good idea. You mentioned that detecting special characters other than those two (Ñ, &) and space would be acceptable. What about tabs? What about new line characters? What about non-latin characters? If you are confident that this would never happen then go ahead with the detection of special characters. If a match is made in the following regex then the input is not valid:

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

The solution that is provided above is good that it checks that the correct characters were used however it will match even if any of the special characters above are used. I guess this is not what you want. Here is a modified version:

^(([A-Za-z]+[^A-Za-z\d&Ñ])|([^A-Za-z\d&Ñ][A-Za-z]+)|([^A-Za-z\d&Ñ][A-Za-z]+[^A-Za-z\d&Ñ]))$

If this matched then the input is valid otherwise it's not. This is probably not the most efficient regex but it should work.

Btw, some sample input text would really help!


Version without capital letters:

^(([A-Z]+[^A-Z\d&Ñ])|([^A-Z\d&Ñ][A-Z]+)|([^A-Z\d&Ñ][A-Z]+[^A-Z\d&Ñ]))$

Uphill_ What '1
Biki
Uphill_ What '1
Biki
Actually it's not correct based on your description unless I didn't understand it clearly. If you require at least one alphabetic character then my regex is the correct one. If you require at least one character but it can be anything including numbers and the 2 special characters then yours is the correct one.
Uphill_ What '1
A: 

Actually I am done with the validation with Jquery alphanumeric plugin

i.e "$('#txtId').alphanumeric({ allow: "&Ñ" });".

Explanation: This allows all alpha numeric characters & restricts all other characters (special characters got addressed). In the allow part I could place the allowable special characters.

Biki