Text box validation using regular expression - asp.net

I have a textbox and have to use the regular expressions in asp.net
My text should not allow the spaces in first and last place. In between words it can allow.
that means: it should allow alphabets, numbers and all special characters.
Output should be:
India Bangalore -valid
India Bangalore - not valid
!India bangalore - valid
India bangalore!##$%- valid
IndiaBangalore - valid
i.e : use can enter the spaces in between the words but not in first position and last position.
Java script also fine.
Thanks in advance.

Here is a quick example
// Input string
string st = " This is an example string. ";
// Call Trim instance method.
// This returns a new string copy.
st = st.Trim();

U can get the index, i mean index(0) is the first position of textbox and calculate the length of ur input +1 = last index of ur input validate white space validation now. damn sure it will work

Related

How to replace certain part of dynamic string

I want to replace a certain part of dynamic string in vb.net. I will try to explain it in this example:
I have a STRING like this:
&12=1005&14=96&230=28&1116=0074005&1271=45&1272%3d001002003%2612%1276=1
and I want this part 3d001002003 to be changed with some other text that I have entered in a Textbox (or any other element).
Problem is that this STRING is sometimes different in character size:
&12=1005&14=96&230=28&1116=0074005&1271=45&1272%3d001002003%2612%1276=1
&12=15&14=96&230=28&1116=0075&1271=45&1272%3d021022023%26276=1... and so on.
Only constant thing in this STRING is that part which needs to be changed always starts with 3d and always has 11 characters. So, something what I'm looking for is to find the part of string that starts with 3d and contains 11 characters and then replace it with some other content from Textbox.
Thanks in advance!
try using below regex
^3d(?!.* )(?=.*[\w-])[\w -]{11}$
(starting with 3d with following total 11 characters)
OR
^%3d[0-9]{9}%
(starting with %3d with following total 9 numeric(assuming only numeric in-between) ending with % = total 11)
in vb.net
example:
Console.WriteLine(Regex.Replace("&12=15&14=96&230=28&1116=0075&1271=45&1272%3d021022023%26276=1", "%3d[0-9]{9}%", "%R%", RegexOptions.IgnoreCase))

Regarding Applying validation in textbox through Regular Expression

I am trying to apply validation in Textbox control for limiting empty space in control. Following is the Regular Expression code I'm using:
Regularexpression validationexpression="^[^-\s][a-zA-Z0-9_\s-]+$" errortext="" />
Now my requirement is:
User should not enter empty space in begining. (Working fine)
Textbox limit is upto 10 numbers, user will able to enter as much as number he wants, no validation if he enter less than 10 numbers. (Working fine.)
validation should prompt if user enter the numbers like this "111
111 ", means show validation if there is empty space between
numbers. (Not working)
Currently I'm using following Regular Expression to achieve this thing, please let me know or update my regular expression so I can achieve this requirement.
Regularexpression validationexpression="^[^-\s][a-zA-Z0-9_\s-]+$" errortext="" />
^[a-zA-Z0-9_-]{1,10}$
Try this.See demo.
http://regex101.com/r/wQ1oW3/23
you can use a regex like
^[^\s][\d\w_-]{1,10}$
which will match as
http://regex101.com/r/lK4pF7/1
how it works?
^ asserts the pattern at the begining of the string.
[^\s] negation of \s validates those without an empty string at the begining
[\d\w_-] ensures that body contains only alphanum, _, - no spaces.
{1,10} minimum 1 and maximum 10 mathes, ensures length not greater than 10
$ asserts the pattern at end of string

RegularExpression Validator For Textbox

In my requirement a Textbox should allow Alphabets,Numeric s, Special Characters,Special Symbols With at least one Alphabet.
I will try like this but i am not getting.
^\d*[a-zA-Z][a-zA-Z0-9#*,$._&% -!><^#]*$
You may want to have 2 regular expression validators; one for validating the allowed characters, and one for validating that at least on alphabet has been provided. You may be able to get at least one, but this way, you can have two separate validation messages to show the user explaining why the input is wrong.
Just match for special characters until you encounter a letter, then match for everything until the end of the string:
^[0-9#*,$._&% -!><^#]*[a-zA-Z0-9#*,$._&% -!><^#]*$
Use lookaheads :
/^(?=.*[a-zA-Z])[\w#*,$.&%!><^#-]*$/
Edit :
I assume the - is meant as the actual - character and not a range of space to !.
I removed the space character. You can of course add it if you want.
[ -!]
Effectively means :
[ -!] # Match a single character in the range between “ ” and “!”
And I have no idea what that range entails!

Text box validation using regular expression in asp.net

I have a textbox and have to use the regular expressions in asp.net
My text should not allow the spaces in first and last place.
Output should be:
Valid:
[India Bangalore]
Not valid:
[ India Bangalore ]
i.e : user can enter the spaces in between the words but not in first position and last position.
If you have solution in JavaScript that is also fine.
Trim() should remove any trailing or leading spaces.
Try this please :
^[^\s].*[^\s]$
It simply match input which:
not to start with any white space ^[^\s] followed by any character even white spaces .* and not end with any white space [^\s]$.
Any way calling Trim() method on input string in server-side is much easy .

Date Regular Expression Validator

I have a regular expression validator on a text box to validate that the text entered is a valid date.
See reg ex below:
ValidationExpression="^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"
Now I want to allow the following in the textbox: mm/dd/yyyy
How can I update my regex so that if mm/dd/yyyy is entered it does not throw a validation error?
Thanks in advance.
ValidationExpression="^[0-9m]{1,2}/[0-9d]{1,2}/[0-9y]{4}$"
Basically allows 0-9 or m in the first field, 0-9 or d in the second, 0-9 or y in the third (in regular expression [] brackets contain a list of possible options, - denote ranges of values when placed within brackets).
This is a more accurate way to restrict the Date to a more meaningful format
^[1-12]{1,2}/[1-31]{1,2}/[2000-2050,1900-1999]{4}$
This is still not perfect as this will allow - for instance - a date 02/31/2013.
Just realized this is quiet buggy.

Resources