Make MaskedEditExtender Mask optional - asp.net

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.

Related

Regular expression, required field only with alphanumeric

So how do I add that it should not have empty field in this same line including my other validation for alphanumeric reg ex.
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
I tried this and didn't work
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$ | /\S/
This is for validation controls
<asp:RegularExpressionValidator id="userLocationValidation" runat="server"
ControlToValidate="userLocation"
ValidationExpression="/\S/"
ErrorMessage="Only use letters from the english alphabet a-z">
</asp:RegularExpressionValidator>
Use:
^[A-Za-z0-9 _]+[A-Za-z0-9][A-Za-z0-9 _]*$
The '+' says one or more, so empty fields will fail.
^[a-zA-Z0-9 _]+$
+ means at least one
if you mean at least one not white-space character something like
^[a-zA-Z0-9 _]*[a-zA-Z0-9_]+[a-zA-Z0-9 _]*$

regular expression - word surrounded by no matter

I'm looking for a regular expression that which will match a string ##test## in a text. The string can be surround by any word or nonword character, or white-space, or newline - so everything can be one the left and on the right of the string, no matter.
I need to use it in a asp.net RegularExpressionValidator control for ValidationExpression.
Thanks in advance for help !
Regards.
<asp:RegularExpressionValidator ID="RegularExpressionValidator9" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" ValidationGroup="Mail" ErrorMessage="Insert !"
ValidationExpression="(\w|\s|\n)##test##(\w|\s|\n)" >*</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button"
ValidationGroup="Mail" Onclick="Button1_Click" />
This should do it:
ValidationExpression="[\s\S]*##test##[\s\S]*"
I can see three problem with your regex, all having to do the (\w|\s|\n) portion:
It will match exactly one character. You need it to match zero or more characters; adding the * quantifier does that.
It's needlessly redundant and gratuitously inefficient. [\w\s] matches the same things: a word character (\w) or a whitespace character (\s, which already includes \n). And whenever you have a choice between using and alternation ((a|b|c)) or a character class ([abc]), the character class should always be the first tool you reach for. It may look like a trivial choice, but it can have a huge impact on performance.
It leaves out a lot of characters, most notably punctuation characters like !, +, ., etc..
If you're wondering why I used [\s\S]* and not .*, it's because the validation can be performed either in the browser or the server. In JavaScript regexes, as in most other flavors, the . metacharacter doesn't match newlines. Most of the others also support a "single-line" or "dot-matches-all" mode, but not JavaScript.
Of course, you could just force it to run on the server only, but you might as well get into habit of dumbing down your regexes to JavaScript's level when you can. :-/
Something like this?
.*(##test##).*

Trouble with the simplest regex

I'm trying to validate an ASP TextBox that can only have 1-2 digits (not zero) using the following regex:
^[1-9]{1,2}
This regex doesn't validate if the field is empty (assumed it would due to the 1-2 part)
Also tried ^[1-9+]{1,2} to no avail.
These are my controls:
<asp:TextBox ID="txtInHour"
MaxLength="2"
runat="server"
Text='<%# Bind("InHour") %>'
Width="80"/>
<asp:RegularExpressionValidator ID="rvInHour"
ControlToValidate="txtInHour"
Display="None"
ValidationExpression="^[1-9]{0,2}$"
runat="server"
ErrorMessage="InHour is incorrectly formatted." />
The first thing I notice is that you don't allow zeros in your pattern. So 10 or 20 is not valid? The second thing is that you start with "starts with" AKA "^" but there's no "ends with" AKA "$"
So.. try this:
^[1-9][0-9]?$
In human readable:
starts with 1-9, followed by an optional digit from 0-9, end of string.
On the other hand I don't know what you've meant with ("no zeros") - no zeros at all?!
I found out that for some reason RegularExpressionValidators don't work when there's no input to match against (blank fields) so I had to use a seperate RequiredFieldValidator. Thanks for your input everyone!
You can use this regex :
^([1-9]|[1-9][0-9])$

Databinding int32 to MaskedEditExtender enabled TextBox

I have a master/detail scheme for editing an asp:GridView using an asp:DetailsView. One of my fields is for a phone number of type int64 (always 10 digits). I would like this field to always be displayed as (###)###-####. My issue is the first digit in the phone number is always truncated for my edit item field which I used a MaskedEditExtender to achieve the formatting.
Here is my EditItemTemplate for the details view:
<cc1:MaskedEditExtender TargetControlID="edtPROJ_Leader_Phone" Mask="(999)999-9999" runat="server" ClearMaskOnLostFocus="false" ClipboardEnabled="true" MaskType="Number" />
<asp:TextBox ID="edtPROJ_Leader_Phone" runat="server" Text='<%# Bind("PROJ_Leader_Phone") %>' ></asp:TextBox>
When my details view is displayed for editing, the text box displays(_23)456-7890 for the integer 1234567890. Also worth noting that if the property MaskType="Number" is removed, the textbox shows: (234)567-890_. I would of course have the textbox show (123)-546-67890 after binding.
Problem could be that you're not using "Escape Characters" for your "(", ")", and "-".
Might want to change your mask from
Mask="(999)999-9999"
to
Mask="\(999\)999\-9999"
According to the documentation, there is no "(", ")", or "-", so you may be telling it to do something unintended. From the section on masks...
/ - Date separator
: - Time separator
. - Decimal separator
, - Thousand separator
\ - Escape character
{ - Initial delimiter for repetition of masks
} - Final delimiter for repetition of masks
Examples
9999999 - Seven numeric characters
99/99 - Four numeric characters separated in the middle by a "/"
http://www.asp.net/ajaxlibrary/act_MaskedEdit.ashx
This issue appears to be a bug related to: http://www.codeplex.com/AjaxControlToolkit/WorkItem/View.aspx?WorkItemId=11819
I had the same issue, and what solved it for me was changing the MaskedEditExtender's property
"ClearMaskOnLostFocus" to True.
I had the same issue too, and :
Doing despecialisation in the mask like :
Mask="\(999\)999\-9999",
With ClearMaskOnLostFocus set to true.
Solved the problem.
Thanks for all.

Regular Expression to test for the number of any characters in a range

I'm using the ASP Validation control a wanted to test that multiline textbox content was between 2 and 40 characters. Any character can be provided, including the new line character. What would be the expression? What I have below fails.
<asp:RegularExpressionValidator ID="RegularExpressionValidator"
runat="server"
ValidationExpression="^[.]{2,40}$"
ControlToValidate="CommentsTextBox"
ErrorMessage="The Comment field must be between 2 and 40 characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
When you put the dot inside square brackets, it loses its special meaning and just matches a literal dot. The easiest way to match any character in an ASP RegularExpressionValidator is like this:
^[\s\S]{2,40}$
[\s\S] is the standard JavaScript idiom for matching anything including newlines (because JS doesn't support DOTALL or s-mode, which allows the dot to match newlines). It works the same in .NET, so you don't have to worry about whether the dot matches newlines, or whether the regex will be applied on the server or the client.
You're treating the period as a literal inside the brackets. You just want:
^(.|\n){2,40}$

Resources