RegularExpression-ValidationExpression on validating aspxtextbox with decimal mask - asp.net

Hi all this is my aspxtextbox design, what I did is I have taken mask along with regular expression validation as follows
<dx:ASPxTextBox ID="txtBalance" runat="server" Width="150">
<MaskSettings Mask="<0..999999999999g>.<00..99>" IncludeLiterals="DecimalSymbol" />
<ValidationSettings RegularExpression-ValidationExpression="^(?:\d{1,14}|\d{1,11}\.\d\d)$">
</ValidationSettings>
</dx:ASPxTextBox>
But this is firing error what I need is I need to validate this input 111,111,111,111.11 which is a maximum input as per my requirement

it is failing because of two reasons the main one is your regex will only accept a decimal place upto the eleventh character and secondly you arent matching the commmas you are putting in to separate your number up.
take a look at thispost to find a regex that would do what your after

Related

Social security Regular Expression validator in asp.net

i want to use regular expression to validate SSN with dashes. This is the format i would like to see: 000-00-0000. This expression does not work for me for some reason.
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ValidationExpression="^(\\d{3}-\\d{2}-\\d{4}|\\d{9})$" ControlToValidate="txtSSN" ForeColor="Red" />
this is how i resolved:
^\d{3}-\d{2}-\d{4}$
There are a few things to note about the RegularExpressionValidator:
It will never be activated by an empty string in the control it is
validating. Only the RequiredFieldValidator catches empty strings You
do not need to specify beginning of string and end of string matching
characters (^ and $)—they are assumed. If you add them, it won't hurt
(or change) anything—it's simply unnecessary.
You could just refer to an example regex on thatpage in MSDN, and see that the value is treated as a verbatim string literal.
So, to only allow SSNs with hyphens, you just need to use:
ValidationExpression="\d{3}-\d{2}-\d{4}"
It is also ECMAScript-compliant and safe to use both at server and client, but you could further limit to
ValidationExpression="[0-9]{3}-[0-9]{2}-[0-9]{4}"
Remember, \d at server-side can also match some non-standard (Arabic, etc) digits.

RegularExpressionValidator to limit input length and allow empty strings

I'm really bad with regex and was looking at another question almost identical to this but can't get it to work as I need.
I was to use a RegularExpressionValidator that will allow any character up to 255 characters or nothing at all. I tried,
ValidateExpression="^.{255}$"
but it throws an unhanded exception when the textbox that I'm checking is empty.
I've also tried
ValidateExpression="^.{,255}$"
Thank you
Rodney
Did you try ^.{0,255}$? Not sure what exception you are getting though.
EDIT: Also, if struggling with .Net regex, Regex Hero tester is a great help. I know there are other sites, but this one is by far the best, if you ask me.
The proper expression is ^.{0,255}$.
^.{255}$ will only match if there is exactly 255 characters, while ^.{,255}$ will match the literal string "{,255}".
If there are still issues after trying that, can you tell us the exception?
If it throws an HttpRequestValidationException exception, you can use the members of the UnvalidatedRequestValues class.
Mind that "Validation succeeds if the input control is empty" (MSDN). You may add a RequiredFieldValidator field to ensure that the user does not leave a text box blank (if you use the RequiredFieldValidator control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel - MSDN):
<asp:textbox id="myTB"
runat="Server">
</asp:textbox>
// ... MAKE SURE YOU DO NOT USE TextMode="Number"!!!
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="myTB"
ErrorMessage="Please enter a number.<br />"
Display="Dynamic"
runat="server"/>
And as for regex, ^.{255}$ means match any character (except newline) exactly 255 times between string start and end. I think it makes sense to allow ^[1-9][0-9]{0,254}$ (for values like '34', '104', etc.).

RegularExpressionValidator only checks once

I have a form in which I would like guests to enter their name, address etc...
On each field I have a RequiredFieldValidator and a RegularExpressionValidator.
For example:
<asp:TextBox ID="mailNameTextBox" runat="server" MaxLength="70" ValidationGroup="mail"></asp:TextBox>
<asp:RegularExpressionValidator ID="mailNameTextBox_RegularExpressionValidator" runat="server" ErrorMessage="Name can only have letters or spaces." ControlToValidate="mailNameTextBox" ValidationExpression="[a-zA-Z' ']" ValidationGroup="mail" Display="Dynamic">*</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="mailNameTextBox_RequiredFieldValidator" runat="server" ErrorMessage="Name field is required." ControlToValidate="mailNameTextBox" ValidationGroup="mail" Display="Dynamic">*</asp:RequiredFieldValidator>
When I enter a name that includes a number (ie. fails the Regex) and "tab" out of the TextBox, the RegexValidator is instantly displayed with a *, and the form fails to submit. But when I go back and remove the number (ie. pass the Regex), the Validator doesn't seem to re-check the input, and the form will forever fail to submit.
Is there a way to always fire the Validation when leaving the box rather than just the first time (besides calling a Validation method upon the TextBox losing focus). I would have thought this would be automatic?
I have spent some time trying to find an answer to this, and I'm not sure how to word my question to "thouroughly research it", so I'm sorry if this is a duplicate.
I did see this one:
ASP.NET: RegularExpressionValidator Doesn't reCheck the input
but it didn't really help in my case.
Your validation expression is this:
[a-zA-Z' ']
Which means: exactly one letter or one apostroph or one space.
You probably want to use something like the following:
[\w\s]*
Which means: any number of letters or whitespaces.

only two digits allowed after decimal asp.net

I have a textbox where the user must not be able to enter more than two digits after a decimal.How do I do this without using javascript?
Thanks.
You can set the MaxLength property of the textbox, but that doesn't have any notion of whether or where the decimal point is.
You could also use a CustomValidator and check the inputted number on the server via the ServerValidate event. But this will require going to the server to check the value (i.e. it will initially look like your form allows users to input invalid numbers).
You should also be able use to a RegularExpressionValidator, depending on your exact globalization requirements, which will use JavaScript on the client to provide immediate feedback:
<asp:TextBox ID="NumberTextBox" runat="server" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="NumberTextBox"
ValidationExpression="\d+(?:(?:\.|,)\d{1,2})?" />
If you want the immediate feedback to the user, you'll need to use a JavaScript based solution.
You cannot cause the textbox to stop accepting text after two decimal places without directly or indirectly using javascript. (This is sometimes called an input mask).
You can, however, allow the user to enter free-form text and validate the text upon postback on the server. You can either automatically round the number for them, or return an error message to the client.
If you really need to prevent the user from entering more than two digits after the decimal point, you'll need to use JavaScript or a server control that implements the JavaScript for you.
However, may make more sense to allow them to enter any number of digits and then catch it on validation (or just round to two digits).

ASP.NET compare end time and start time with customvalidator

I have two text boxs for time input. I'm trying to compare two time with format : 10:14:23 AM. Then End-TIme must be greater or equal to Start-Time. The ASP.NET CompareValidator doesnt offer the time type, so I'm thinking of using the Customvalidator. I never use Customvalidator to compare the two text boxs for time input before. How to compare the two time? Thank you.
Have you looked at the MSDN entry for the CustomValidator? It specifies usage and gives an example of how to use it:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
<asp:CompareValidator
ID="ClosedTime_CompareValidator"
runat="server"
ControlToCompare="OpenedTimeTextBox"
ControlToValidate="ClosedTimeTextBox"
ValidationGroup="Submit"
Operator="GreaterThanEqual"
Display="Dynamic">
<img
src="./FormImages/warning.gif"
alt="The entry is not recognizabledate."/>

Resources