Thousand separator with MaskedEditExtender from the ASP.NET AJAX Control Toolkit - c#

I need to format some inputs in an Textbox and I've tried to do so with the MaskedEditExtender. I don't know what mask I have to use to get what I want - maybe somebody knows.
The entered values are only numeric values with amounts between 100 and 9999999 Euros an I want to show the thousand separators an cent separators while typing into the textbox like this:
Input: 100
Show: 100,00
Input: 345000,50
Show: 345.000,50
To be more specific I want the exact same behavior like typing numeric values into an calculator and the kind the calculator shows the entered values on the display.
Here is an (expensive) example with exact the behavior I need for free: https://demos.devexpress.com/ASPxEditorsDemos/Features/MaskedInput.aspx
This is my code:
<asp:TextBox ID="purposeAmount" CssClass="textBoxCreateItem" OnTextChanged="purposeAmount_OnTextChanged" AutoPostBack="true" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="purposeAmount" Display="Dynamic" SetFocusOnError="true" ErrorMessage="Only numeric allowed." ForeColor="Red" ValidationExpression="^\d+(\,\d+$)?$" ValidationGroup="NumericValidate"><br />Erlaubte Zeichen: 0-9 und ,</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" ControlToValidate="purposeAmount" ValidationGroup="NumericValidate" Display="Static" ForeColor="Red"><br />Eingabe erforderlich.</asp:RequiredFieldValidator>
<cc1:MaskedEditExtender TargetControlID="purposeAmount" MaskType="Number" Mask="9,999.99" runat="server" />

Try this:
<ajax:MaskedEditExtender ID="MaskedEditExtender2" Mask="9,999.99"...</ajax:MaskedEditExtender>

Related

How to limit maximum and minimum number of text in asp.net textbox?

I am trying to limit the number of text into a Textbox field. The requirement is to fix the minimum length as 7 and maximum length as 15.
I have tried to use the asp:RegularExpressionValidator to limit the number of text into the textbox.
Here is my code:
<asp:RegularExpressionValidator ID="revphone" runat="server"
Font-Size="16px"
ValidationExpression="^[0-9]{7,15}$"
ControlToValidate="txtphonenumber"
ErrorMessage="*"
ValidationGroup="reg"
Display="Dynamic"
ForeColor="Red">
</asp:RegularExpressionValidator>Phone Number
<asp:TextBox ID="txtphonenumber" runat="server"
placeholder="Phone Number"
Width="305px"
onblur="BtnRegisterClick()"
MaxLength="7">
</asp:TextBox>
I am able to get the minimum number but not getting the maximum number. It is limiting to type 7 digits only.
Here is Your Solution:-
<asp:TextBox ID="txtphonenumber" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "txtphonenumber" ID="revphone" ValidationExpression = "^[\s\S]{7,15}$" runat="server" ErrorMessage="Minimum 7 and Maximum 15 characters required."></asp:RegularExpressionValidator>
For More Info Refer this Below Link.....
https://www.aspsnippets.com/Articles/TextBox-Minimum-and-Maximum-Character-Length-Validation-using-ASPNet-RegularExpression-Validators.aspx

How to write a RegularExpressionValidator for a 10 Digits mobile number?

I am a new ASP.NET Webforms developer and I am struggling right now with validating the TextBox control that asks for a mobile phone number. There is only one format used when entering a phone number which is
05# ### ####
I would like to use ASP.NET RegularExpressionValidator control with this TextBox and I need to a regular expression that matches the preceding expressions. The regular expression should validate the entered number against the following rules:
The preceding format
The number must start with 05
The number must be of 10 digits only
So how can I write a regular expression (or ValidationExpression) property for this ASP.NET RegularExpressionValidator control?
Here's a snippet of my work:
<asp:TextBox ID="txtPhoneNew" runat="server" CssClass="form-control" placeholder="966 ## ### ####"></asp:TextBox>
<asp:RegularExpressionValidator ID="revPhoneNew" runat="server"
ControlToValidate="txtPhoneNew" ErrorMessage="Enter a valid mobile phone number"
ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
as Tim stated, and per your example the regular expression you need is
<asp:TextBox ID="txtPhoneNew" runat="server" CssClass="form-control" placeholder="966 ## ### ####"></asp:TextBox>
<asp:RegularExpressionValidator ID="revPhoneNew" runat="server"
ControlToValidate="txtPhoneNew" ErrorMessage="Enter a valid mobile phone number"
ValidationExpression="^05\d{3}\s\d{4}$"></asp:RegularExpressionValidator>
try this
<asp:TextBox ID="txtPhoneNew" MaxLength="10" runat="server" CssClass="form-control" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtPhoneNew"
ErrorMessage="enter valid phone" ValidationExpression="^05[0-9]{8}$"></asp:RegularExpressionValidator>

ASP.NET date validator passing when it should not

I'm using a RegularExpressionValidator in ASP.NET. I have these validators for my asp:Textbox control:
<asp:RangeValidator
ID="rvDate"
runat="server"
ControlToValidate="txtCrimeDate"
Type="Date"
ErrorMessage="Must be within latest three months"
Display="Dynamic"
OnInit="RangeValidator_Init">
</asp:RangeValidator>
<asp:RegularExpressionValidator
ID="revDate"
runat="server"
ControlToValidate="txtCrimeDate"
Display="Dynamic"
ErrorMessage="Must be in format YYYY-MM-DD"
ValidationExpression="[0-9]{4}-[0-9]{2}-[0-9]{2}">
</asp:RegularExpressionValidator>
I have set the min and max value of the RangeValidator in the code-behind and it works as intended. But! If I enter something with 2 numbers for the year, like 15-11-28 everything passes, even the other validators for the other controls. Any ideas?
Check the date format (US or otherwise) and 0-9 for the day and month are incorrect eg:
2013-99-99
Look into using the CompareValidator control, this is mainly used for string matching, but can be made to check between two date ranges.

How can i prevent only html tags which does not include numbers, so how can i allow numbers

I set validation expression ValidationExpression="<(.|\n)*?>" but it does not give permission to numbers how can i solve it?
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"></asp:RegularExpressionValidator><br />
Instead of (.|\n)*, use \D*.

Why isn't my RangeValidator working?

I succesfully used a validator more than once,but after some programming my validators are not working.Maybe is something i don't know about defining 2 validators for the same control, but it doesn't work for one validator in a control either.Here are 2 examples of my code:
Example 1: one required field validator and one "maximum value" validator for username:
<asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Dynamic" ErrorMessage="Required" />
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="UsernameTextbox" MinimumValue="1" MaximumValue="20" ForeColor="red"
Display="Dynamic" ErrorMessage="Name must contain maximum 20 characters"></asp:RangeValidator>
Example 2: one "maximum value" validator for roadaddress(string):
<asp:RangeValidator ID="RangeValidator9" runat="server" MaximumValue="50" ForeColor="red"
ErrorMessage="Road Address must contain maxmum 50 characters" ControlToValidate="RoadAddressTextbox"></asp:RangeValidator>
I am thinking that the problem is maybe in the display property or in the causesvalidation property which i don't use...
That's not what the RangeValidator is used for. The RangeValidator is intended to check input to make sure it's within a certain range, i.e. to make sure that a number is between 1 and 5, that a date is within a certain range, etc.
What you need is a RegularExpressionValidator:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="UserNameTextbox"
ErrorMessage="Username must be between 1 and 50 characters"
ValidationExpression="^[a-zA-Z\s]{1,50}">
</asp:RegularExpressionValidator>
EDIT: Updated expression to ^[a-zA-Z\s]{1,50}
RangeValidators don't validate the number of characters in the input, they "Check whether the value of an input control is within a specified range of values."
You can actually do this without a Validator, by setting the MaxLength property on the text box which would limit the number of characters entered into it.
<asp:TextBox ID="UserNameTextbox" MaxLength="50" runat="server"></asp:TextBox>

Categories