ASP.NET compare end time and start time with customvalidator - asp.net

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."/>

Related

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.).

RangeValidator currency validation makes no sense

I'm having some issues using a RangeValidator in ASP.NET 4.5. The issue I'm facing is trying to validate a range of currency values.
Let's say I have the following RangeValidator:
<asp:RangeValidator ID="_rngValCustomAmount" runat="server"
CssClass="error-message" ErrorMessage="Please enter a valid amount."
ControlToValidate="_txtCustomAmount" MinimumValue="10.00"
MaximumValue="500.00" Type="Currency" />
When I enter a value of '450' the validation is passed, and I can submit the form. However, if I enter '$450' then the validation fails, even though I have set the Type property to Currency.
So, then I thought, OK, maybe I need to enter a currency Minimum and Maximum value, however when I change the control to:
MinimumValue="$10.00"
MaximumValue="$500.00"
I get the following .NET exception:
The value '$500.00' of the MaximumValue property of '_rngValCustomAmount' cannot be converted to type 'Currency'.
I don't really want to have to resort to using a CustomValidator for this, so that I don't have to write a JS function for something that should exist as the RangeValidator supports the currency type. Can anyone point out what I might be doing wrong?

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.

Should Regular Expressions be used over other validation methods?

Given a Textbox, that should contain a numeric value:
<asp:TextBox ID="txtHoldsAnInt" runat="server" />
In our codebase, generally a RegularExpressionValidator is used for all validation (except required fields) such as the following:
<asp:RegularExpressionValidator ErrorMessage="..." ControlToValidate="txtHoldsAnInt"
Text="*" runat="server" ValidationExpression="^[0-9]{1,8}$" />
Alternately, a RangeValidator could be used to get the same result:
<asp:RangeValidator ErrorMessage="..." ControlToValidate="txtHoldsAnInt"
MinimumValue="0" MaximumValue="99999999" Type="Integer" runat="server" />
Does the RegularExpressionValidator have an advantage over other validators, even when another validator would work? Are there any advantages to always using a RegularExpressionValidator?
If you are familiar with regular expressions, then I would recommend using the <asp:RegularExpressionValidator. You are correct that there is no noticeable difference when they are equal to each other, but the RegularExpressionValidator allows for easy editing later on when specifications change. For example with a RegularExpressionValidator it would be easy to allow for a percent sign at the end or any other change that might be requested.
In terms of functionality/end result, there is no difference between using the two. I would suggest using the range validator other validators if the situation permits and it satisfy your needs. It is much easier for someone to understand <asp:RangeValidator than to understand ^[0-9]{1,8}$. While the RegularExpressionValidator will give you a much higher degree of control, if that control is not needed, then go for the simple solution and ease your maintenance efforts.

RegularExpression-ValidationExpression on validating aspxtextbox with decimal mask

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

Resources