Sep 21, 2007

Input String Validation

What if your validation logic just requires allowing only alphanumeric string with some special characters

Lets say it must not allow any special characters except (dollar sign $, underscore _, hyphen - , asterisk* and question mark?)

There could be many ways to implement the logic, but I love to do it using regular expressions.

So just use RegEx ValidString = new RegEx("^[-_A-Za-z0-9\?\*\*]$"); to verify the input is a valid string.

Ok job done, but what if you needs to identify the invalid character in user input.
The trick could be too just use the negative of above Regex.

RegEx InValidSplCharacter = new RegEx("[^-_A-Za-z0-9\?\*\*]");

So small way to do big things, this is what I love about Regular expression.

No comments: