Issue with Regular Expression Validation - asp.net

I have an Asp.Net script which includes a validator like this:
<asp:RegularExpressionValidator ID="AdvNeedIdValidator" runat="server"
ControlToValidate="NEEDS" ErrorMessage="Need ID is numeric."
ForeColor="Red" ValidationExpression="^\d*\.?\d*$">*</asp:RegularExpressionValidator>
The validator is supposed to reject non-numeric inputs (NEEDS is a numeric value) but I think there is problem with the regular expression "^\d*\.?\d*$" because when I want to clear the Input text the when the user enter the text the ErrorMessage pops up. I have a NeedID field with 4 number numeric value,so can you please let me know how I can upgrade the "^\d*\.?\d*$" to get rid of that Issue?
Thanks

If you simply want to validate a 4 digit whole number, try using the following:
^\d{4}$
^\d means start with any digit
{4} means there must be exactly 4.
$ means that is the end of the pattern.
So in other words, the regex validates and expression of exactly 4 digits.

If you're looking to validate numbers such as:
500.40
30
0.02
.02
this should meet what you're looking for:
"\d*\.?\d+"

Related

Make MaskedEditExtender Mask optional

Here I have a MaskedEditExtender, with a validator using a regex.
It validates phone numbers with 8 or 9 digits:
<asp:TextBox Style="width: 135px" ID="txtTelefone" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender
ID="MaskedEditExtender_Telefone"
TargetControlID="txtTelefone"
runat="server"
Mask="\(99\)9999NN9999"
OnInvalidCssClass="txt-TextBox-Error"
ValidateRequestMode="Enabled"
ErrorTooltipEnabled="True"
Filtered="-"
PromptCharacter=" "
ClearMaskOnLostFocus="false"/>
<ajaxToolkit:MaskedEditValidator
ID="MaskedEditValidator_Telefone"
runat="server"
ControlExtender="MaskedEditExtender_Telefone"
ControlToValidate="txtTelefone"
ValidationExpression="^\(\d\d\)\d\d\d\d+-\d\d\d\d$"
Display="Dynamic"></ajaxToolkit:MaskedEditValidator>
The issue is: as you can see in the regex, the user can put 4 or 5 digits between the ')' and the '-'.
But the "Mask" field doesn't allow it.
I need the MaskedEditExtender to stop crying when I don't type all the characters, because they're not necessary. All I need to validate my field is the regex.
The MaskedEditExtender is there only to give a mask that allows me to type only numbers and have a (99) in the beggining. It does not need to validate anything.
Well, seems like there's no such functionality in MaskedEditExtender that allows you to put less than the characters in the mask, so I did a small workaround:
I've put autocomplete in the mask, adapted the regex to accept an empty space in the end of the string and trimmed it everytime I wanted to use the TextBox's value.
In MaskedEditValidator:ValidationExpression="^\(\d{2}\)\d{4,5}-\d{4} *$"
In MaskedEditExtender:AutoComplete="true" AutoCompleteValue=""
In CodeBehind: txtTelefone.Text.Trim();
With this, all unfilled characters will be replaced as space in the end of the string, the regex will take care of the validation, and the Trim() will remove the spaces. Thus allowing you to make the Mask's length optional.
Set ClearMaskOnLostFocus="true" on MaskedEditExtender.

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

Textboxvalidation: Empty or minimum 3 characters (spaces do not count)

I have some textboxes I'm using as search-fields.
The textbox can be empty, but when a search-criteria is filled in, it must be at least 3 characters long, ignoring the spaces in the count.
I've found that a regularexpressionvalidator validates true when the textbox is empty, so that part is ok.
Q: regex for a minimumlenth of 3 characters. Spaces are allowed, but should not count in the length.
Thanks.
Have you tried something like this?
'(\s*\w\s*){3}'
This regular expression looks for a character (\w) optionally surronded by any whitespace (\s*) three times ({3}), which is what you're looking for.
Note: I don't know asp.net, but I think the regular expression is all you need to solve the problem.

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!

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