开发者

How to check input string with no spaces & special characters except two special charaters (that is & and Ñ)

开发者 https://www.devze.com 2023-01-20 18:53 出处:网络
I am trying to validate an input string开发者_运维问答 (which can be copied & pasted in the textbox).

I am trying to validate an input string开发者_运维问答 (which can be copied & pasted in the textbox). The input entered should contain one or more alphabetic characters, numbers and two more special characters as (& and Ñ), but no embedded spaces.

NB: Alphabetic characters 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 occurrence of any special characters and spaces, except '&' and 'Ñ' in any input string, then also my problem can be resolved.


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&Ñ]))$


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消