MaskedEditValidator not working on client side - c#

I am trying to enforce 15-minute granularity for time information entered by the user. So, for example, 12:15 PM and 3:45 am and 9:30 A.M. are all acceptable, but 2:35 PM would not be allowed. Server-side validation works, but it would be nice if the user was told their input was invalid when the text box loses focus, before they click the submit button. Here is the code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True" UpdateMode="Always">
<ContentTemplate>
<asp:TextBox ID="txtStartTime" runat="server" AutoPostBack="True" CausesValidation="True"/>
<Ajax:MaskedEditExtender ID="txtStartTime_MaskedEditExtender" runat="server"
TargetControlID="txtStartTime" MaskType="Time" AcceptAMPM="True"
Mask="99:99">
</Ajax:MaskedEditExtender>
<asp:RequiredFieldValidator runat="server" ID="StartTimeRequired"
ValidationGroup="EventAddEditControls" ControlToValidate="txtStartTime"
EnableClientScript="True" SetFocusOnError="True">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Invalid time format." ControlToValidate="txtStartTime"
ValidationGroup="EventAddEditControls" SetFocusOnError="True"
EnableClientScript="True" Text="Invalid time format."
ValidationExpression="^([1-9]|0[1-9]|1[012]):(00|15|30|45)\s?[aApP]\.?[mM]\.?$" />
<Ajax:MaskedEditValidator ID="MaskedEditValidator1" ControlToValidate="txtStartTime"
ControlExtender="txtStartTime_MaskedEditExtender" IsValidEmpty="False"
ValidationGroup="EventAddEditControls"
ValidationExpression="^([1-9]|0[1-9]|1[012]):(00|15|30|45)\s?[aApP]\.?[mM]\.?$"
EnableClientScript="True" SetFocusOnError="True" Text="Time format is invalid."
runat="server"></Ajax:MaskedEditValidator>
</ContentTemplate>
</asp:UpdatePanel>
How can I get the MaskedEditExtender to enforce the 15-minute granularity constraint on the client side also (assuming that this is possible)?

This seems overly simple, but it seems to work.
The first thing I did was to comment out the RegularExpressionValidator - you don't really need both this and the MaskedEditValidator. To prove the point, apply the fix below but don't comment out the RegularExpressionValidator. Upon a failed validation you will see both errors.
The last thing was to replace the Text property of the MaskedEditValidator with the InvalidValueMessage property:
<Ajax:MaskedEditValidator ID="MaskedEditValidator1" ControlToValidate="txtStartTime"
ControlExtender="txtStartTime_MaskedEditExtender" IsValidEmpty="False"
ValidationGroup="EventAddEditControls"
ValidationExpression="^([1-9]|0[1-9]|1[012]):(00|15|30|45)\s?[aApP]\.?[mM]\.?$"
EnableClientScript="True" SetFocusOnError="True"
InvalidValueMessage="Time format is invalid." runat="server">
</Ajax:MaskedEditValidator>
This example gave me the hint regarding the correct property to use.
Doing both of these things resulted in the validation occurring when tabbing out of the control. This also indicates that the MaskedEditValidator was validating all along.

Related

Place holder text is missing and back space not working in masked text box

I have used the masked textbox in asp.net c# application
<div class="span3">
<asp:TextBox ID="txtExpiryDate" placeholder="(mm-dd-yy)" CssClass="datepiker" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
Display="Dynamic" ControlToValidate="txtExpiryDate"><b>Enter Renewal Date</b></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator Display="Dynamic" ID="valdRegStartDate" runat="server"
ControlToValidate="txtExpiryDate" ErrorMessage="Enter a valid Expiry Date." ValidationExpression="([1-9]|0[1-9]|1[012])([-/.])([1-9]|0[1-9]|[12][0-9]|3[01])([-/.])(19[5-9][0-9]|20[0-4][0-9])"><b>Enter a valid Expiry Date.</b>
</asp:RegularExpressionValidator>
<cc1:MaskedEditExtender ID="MaskDate" Enabled="true" runat="server" MaskType="Date"
Mask="99-99-9999" TargetControlID="txtExpiryDate" MessageValidatorTip="true" ClearMaskOnLostFocus="false">
</cc1:MaskedEditExtender>
</div>
The placeholder (mm-dd-yy) in the textbox is not displayed and backspace is not working.
It displays placeholder as __-__-____ instead of (mm-dd-yy).
Please help me !!!
Remove placeholder in your markup and try this in your Page_Load:
txtExample.Attributes.Add("placeholder","mm/dd/yyyy");
The MaskedEditExtender is overriding your placeholder. That's why you're seeing __-__-____ (according to the mask). You can't really have both so one will have to give. You already have the regular expression validator (which is checking for a 4-digit year) so I suggest removing the MaskedEditExtender and tweaking your validator.
I'd even go as far as saying using a CustomValidator to clean up the markup a little.

Error when using RequiredFieldValidator control for FreeTextBox control

In my aspx page:
...
<tr>
<asp:Label ID="FailureText" runat="server" ForeColor="#CC3300"></asp:Label>
<asp:ValidationSummary ID="Alert" runat="server" CssClass="failureNotification" HeaderText=""/>
<tr/>
<tr>
<FTB:FreeTextBox id="FTB" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FTB"
CssClass="failureNotification" ErrorMessage="Content cannot be empty." ToolTip="Content cannot be empty." ></asp:RequiredFieldValidator>
<tr/>
The first time, the code works fine when I let the FTB empty --> FailureText="Content cannot be empty.";
The 2nd time, I press space to input many spaces in FTB --> FailureText doesnt show and the program does the next codes.
I have used RequiredFieldValidator control before but it worked fine for both null or space value.
Help! I really dont know why the RequiredFieldValidator accept space value here???
Maybe this will help. Its not exactly the same, but it sounds like you need to check if only spaces have been entered.
validation on textbox (no space)
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txtBox"
ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
edit...
If you are ok with doing some work server side, this would be an easier solution...
if(string.IsNullOrWhiteSpace(Textbox1.Text))
{
lblError.Text ="Enter required field";
}

Validation control not firing on form submit

I'm using a FormView control to allow users to insert rows to the database. I want to validate these input fields, and as such have added a regular expression validation helper. Here's the markup:
<InsertItemTemplate>
<p>
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9 ]*$" ControlToValidate="NameTextBox" ID="NameTextBoxValidator" runat="server" ErrorMessage="Must be alphanumeric characters and spaces"></asp:RegularExpressionValidator>
</p>
<p>
Location:
<asp:TextBox ID="LocationTextBox" runat="server"
Text='<%# Bind("Location") %>' />
</p>
<p>
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
</p>
</InsertItemTemplate>
However, when I click InsertButton the page refreshes and I get an error from SQL Server saying it can't insert a NULL value, the validator isn't getting used at all.
How can I fix this?
I assume that the user entered no text and the database does not allow null values.
A RegularExpressionValidator will not validate empty controls. So you need to provide also a RequiredFieldValidator.
The validation will not fail if the input control is empty. Use the
RequiredFieldValidator control to make the field required.
http://www.w3schools.com/aspnet/control_regularexpvalidator.asp
Not much info here, but I'll venture a guess:
Check to make sure you don't have anything happening OnLoad that's blanking things out. If you do have an OnLoad make sure it only fires when IsPostback is false.

Use of a RadInputManager (telerik)

i'm trying to use this control to check a time (HH:MM) a user inserts...
This is what i'm using but not working
<telerik:RadTextBox ID="txtEstimation" runat="server">
</telerik:RadTextBox>
<telerik:RadInputManager ID="RadInputManager1" runat="server">
<telerik:RegExpTextBoxSetting EmptyMessage="EmptyMessage" ValidationExpression="(([01][0-9])|(2[0-3])):[0-5][0-9]"
ErrorMessage="*">
<TargetControls>
<telerik:TargetInput ControlID="txtEstimation" />
</TargetControls>
</telerik:RegExpTextBoxSetting>
</telerik:RadInputManager>
I was inspired by this : http://www.telerik.com/help/aspnet-ajax/input-inputmanager-basics.html
Thanks in advance for your help
The purpose of the RadInputManager is to convert standard controls to RadControls at runtime. Therefore, you should either have the RadInputManager OR the RadTextBox, but you don't need both.
You can achieve the HH:MM functionality you want with either way you choose. For example:
<telerik:RadTextBox ID="RadTextBox1" runat="server">
</telerik:RadTextBox>
<asp:RegularExpressionValidator
id="txtValidator"
runat="server"
Display="Dynamic"
ErrorMessage="Please, enter valid time in HH:MM format."
ValidationExpression="^((?<Hour>[0-9]{1,2})[.:](?=[0-9]{2}))?(?<Minute>[0-9]{1,2})$"
ControlToValidate="RadTextBox1">
</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
http://www.telerik.com/help/aspnet-ajax/input-textbox-limiting-allowable-values.html

CausesValidation is set to "False" but the client side validation is still firing

I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on the client side when I press the Cancel button, which has the CausesValidation attribute set to "False". How can I get this to stop?
I do not believe that Validation Groups are supported in 1.1.
Here's a code sample:
<asp:TextBox id="UsernameTextBox" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="UsernameTextBoxRequiredfieldvalidator" ControlToValidate="UsernameTextBox"
runat="server" ErrorMessage="This field is required."></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="UsernameTextBoxRegExValidator" runat="server" ControlToValidate="UsernameTextBox"
Display="Dynamic" ErrorMessage="Please specify a valid username (6 to 32 alphanumeric characters)."
ValidationExpression="[0-9,a-z,A-Z, ]{6,32}"></asp:RegularExpressionValidator>
<asp:Button CssClass="btn" id="addUserButton" runat="server" Text="Add User"></asp:Button>
<asp:Button CssClass="btn" id="cancelButton" runat="server" Text="Cancel" CausesValidation="False"></asp:Button>
Update: There was some dynamic page generating going on in the code behind that must have been messing it up, because when I cleaned that up it started working.
Validation Groups were not added to ASP.NET until version 2.0. This is a 1.1 question.
Double check your setting and make sure you are not overwriting it in the code behind.
Are they in separate validation groups (the button and validator controls)?
You're not manually calling the JS to do the client validation are you?

Categories