asp:MaskedEditExtender stops validator working - asp.net

I have a RequiredFieldValidator. The code is below:
<asp:TextBox runat="server" ID="phone" MaxLength="10" Columns="10"
Width="90px"></asp:TextBox>
<asp:MaskedEditExtender ID="phone_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" TargetControlID="phone" Mask="(999)999-9999" ClearMaskOnLostFocus="False">
</asp:MaskedEditExtender>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidatorPhone" ControlToValidate="phone"
ErrorMessage="Phone is required." Display="Dynamic" InitialValue="" />
If I remove the asp:MaskedEditExtender, the RequiredFieldValidator functions as expected. Otherwise it seems the validator loses its functionality.
Why?
Thanks.

Your MaskedEditExtender isn't doing anything but declaring a mask, and putting itself in the place of the value of your input.
All those Cultures are not needed in the case of a phone number.
What is the validation expression?
The whole point of the MaskedExtender is to control the input type. If you are using ASP you can do this by pulling from database and restricting the max length of the field.
Here is an example.
<cc1:MaskedEditExtender ID="MaskedEditExtender1"
runat="server"
TargetControlID="txtClientLookupValue"
Mask="999999-9999"
ClearMaskOnLostFocus="false">
</cc1:MaskedEditExtender>
<cc1:MaskedEditValidator ID="MaskedEditValidator1"
ControlExtender="MaskedEditExtender1"
runat="server"
ControlToValidate="txtClientLookupValue"
IsValidEmpty="True"
InvalidValueMessage="INVALID"
ValidationExpression="^[2-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$"
Display="Dynamic">
</cc1:MaskedEditValidator>
And here is a reference:
MaskedEditExtenderLinkASP
Good Luck.

Here is how I do phone numbers in ASP.NET (mask and validation). Just trying to help!
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="txb_HomePhoneNumb"
Cssclass="BoxStyleHireAppCityStateZip" runat="server"
Width="175" MaxLength="12">
</asp:TextBox>
<%--MaskedEditExtender Created with: ASP.NET AJAX Control
Toolkit: https://ajaxcontroltoolkit.codeplex.com--%>
<cc1:MaskedEditExtender ID="MaskedEditExtenderHomePhoneNumb"
runat="server"
TargetControlID="txb_HomePhoneNumb"
ClearMaskOnLostFocus ="false"
MaskType="None"
Mask="(999) 999-9999"
MessageValidatorTip="true"
InputDirection="LeftToRight"
ErrorTooltipEnabled="True"
/>
<asp:RegularExpressionValidator runat="server"
ControlToValidate="txb_HomePhoneNumb" style="color:red;"
CssClass="display-next"
ErrorMessage="*Not a valid phone number!"
ValidationExpression="^\D?(\d{3})\D?\D?(\d{3})\D?
(\d{4})$" Font-Size="Medium">
</asp:RegularExpressionValidator>
</div>

Related

ASP .NET CompareValidator control locks everything

I have a form with a field that has a asp:CompareValidator control which checks if the text in the input is of type Integer.
The problem is that the form has some other buttons to navigate to other pages, so when the CompareValidator triggers it locks those buttons until the input in the controled field is Integer although it's not necessary to navigate.
Here's the code:
<asp:TextBox runat="server" ID="txtRelacion" placeholder="Id del evento"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtRelacion" CssClass="compare-validation-error" />
<asp:Button runat="server" ID="submitButton" Text="Submit Form"/>
<asp:Button runat="server" ID="navigateButton" Text="Navigate somewhere"/>
Is this the expected behaviour of the asp:CompareValidator control?
You can specify a ValidationGroup in your button and in your CompareValidator. Then the CompareValidator will be used only for the corresponding ValidationGroup:
<asp:TextBox runat="server" ID="txtRelacion" placeholder="Id del evento"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtRelacion" ValidationGroup="form" CssClass="compare-validation-error" />
<asp:Button runat="server" ID="submitButton" ValidationGroup="form" Text="Submit Form"/>
<asp:Button runat="server" ID="navigateButton" Text="Navigate somewhere"/>

How to make validator's message appear nearby another control?

I have a validator with a bound text field (it's set via ControlToValidate). How can I make the validator's error message appear nearby another control (a label above this text field)?
Just put the validator control nearby the control where you want to show the message i.e. wherever you want to show the message just put the validator control there.
In the following example I am showing validation message near some other control not near the text box.
<form id="form1" runat="server">
<asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
<asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
<br />
<asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
<asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
<asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="0">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
Display="Dynamic" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
ValueToCompare="-1"></asp:CompareValidator>
<br />
<asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
Display="Dynamic" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
<br />
</form>
Hope this helps you.

asp.Net ValidationGroup validating correctly except on Enter key

I've seen a variety of questions here that are very close to this issue but none which seem to fit this specific scenario.
Description:
I have ID and Password fields with a Log On button in the Master page. These are in the "LoginValidationGroup" and they work fine.
Now, in the Default.aspx, I have an email TextBox and submit button that are in the "NotifyMeValidation" group, and they also work, BUT not if you hit the enter key rather than the submit button. The Submit button works fine - Enter key ... not so much.
The Enter key causes the validation to occur on the LoginValidationGroup, even though the TextBox is set to CausesValidation="true", and it is in the NotifyMeValidation group.
I guarantee people are going to enter an email in that box and hit Enter. I would!!! And when they do, they're going to see a callout balloon at the top telling them the User ID is required.
What's my error here? If I need to post the actual code I can do so.
Actually, let me just go ahead and do that.
From the Default.aspx:
<asp:RequiredFieldValidator
runat="server"
ValidationGroup="NotifyMeValidation"
ID="EmailRequiredValidator"
ControlToValidate="SenderEmailTextBox"
ErrorMessage="Email is Required"
Display="None" />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
PopupPosition="Left"
ID="EmailRequiredValidatorExtender"
Width="185px"
TargetControlID="EmailRequiredValidator"
WarningIconImageUrl="/Images/warning.gif" />
<asp:Label ID="SenderEmailLabel" runat="server" ssociatedControlID="SenderEmailTextBox" EnableViewState="false" Text="Email:"></asp:Label>
<asp:TextBox ID="SenderEmailTextBox" runat="server" ValidationGroup="NotifyMeValidation" Width="350" BackColor="#cccccc" CausesValidation="true"></asp:TextBox>
<br /><br />
<asp:Button ID="SubmitButton" runat="server" ValidationGroup="NotifyMeValidation" EnableViewState="false" CssClass="submit" Text="Send" CausesValidation="true" OnClick="btnSubmit_Click" />
From the Master page:
<asp:Label CssClass="LoginHeading" ID="UserNameLabel" runat="server" AssociatedControlID="UserNameTextbox">UserName: </asp:Label>
<asp:TextBox CssClass="LoginTextBox" ID="UserNameTextbox" runat="server" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>
<asp:Label CssClass="LoginHeading" ID="PasswordLabel" runat="server" AssociatedControlID="PasswordTextbox">Password: </asp:Label>
<asp:TextBox CssClass="LoginTextBox" ID="PasswordTextBox" runat="server" TextMode="Password" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>
<asp:Button CssClass="LoginHeading" ID="LoginButton" runat="server" Text="Button" CommandName="Login" ValidationGroup="LoginValidation" CausesValidation="True" onclick="LoginButton_Click" />
And the Master page validators...
<asp:RequiredFieldValidator runat="server" ID="UIDRequired"
ValidationGroup="LoginValidation"
ControlToValidate="UserNameTextbox"
Display="None"
ErrorMessage="<b>Required Field Missing</b><br />A User ID is required." />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="UIDValidationExtender"
PopupPosition="Left"
Width="185px"
TargetControlID="UIDRequired"
WarningIconImageUrl="/Images/warning.gif" />
<asp:RequiredFieldValidator runat="server" ID="PWRequired"
ValidationGroup="LoginValidation"
ControlToValidate="PasswordTextbox"
Display="None"
ErrorMessage="<b>Required Field Missing</b><br />A password is required." />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="PWValidationExtender"
PopupPosition="Left"
TargetControlID="PWRequired"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
Display="None"
ValidationGroup="LoginValidation"
ID="CustomUserExistsValidator"
ControlToValidate="UserNameTextbox"
ErrorMessage="<b>Invalid UserID!</b><br />User ID doesn't exist. Please try again.<br />"
OnServerValidate="CustomCheckUserExists"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="UserIDCalloutExtender"
TargetControlID="CustomUserExistsValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
ID="CustomPWValidator"
Display="None"
ValidationGroup="LoginValidation"
ControlToValidate="PasswordTextbox"
ErrorMessage="<b>Invalid Password!</b><br />Please try again.<br />"
OnServerValidate="CustomValidatePassword"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="PWCalloutExtender"
TargetControlID="CustomPWValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
ID="CustomUserApprovedValidator"
Display="None"
ValidationGroup="LoginValidation"
ControlToValidate="UserNameTextbox"
ErrorMessage="<b>Approval Error!</b><br />This UserID exists, but is not yet approved.<br />"
OnServerValidate="CustomCheckUserApproved"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="UserApprovedCalloutExtender"
TargetControlID="CustomUserApprovedValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
Try adding an <asp:Panel> around each set of controls and setting the DefaultButton property to the ID of the button you want to click when the users hits Enter.
Just to explain what is happening here, the aspx's default button is being fired when you press enter on the textbox. To handle this you could you could fire some jQuery code when the enter key is clicked on the textbox, this code will then perform the click on the specific button that you require. Check this

date validation in asp.net

is this following a proper date validation fn
<asp:TextBox ID="date" Width="80px" MaxLength="10" runat="server" />
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="date"
ErrorMessage="date. " ValidationExpression="^(\d{4})(\d{2})(\d{2})$" Display="Static" Font-Names="Arial"
Font-Size="11" runat="server">
enter a valid date formate
</asp:RegularExpressionValidator>
I believe this would be a better approach, use what's built-in:
<asp:TextBox ID="date" Width="80px" MaxLength="10" runat="server" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="date" ErrorMessage="* Enter a valid date"
Operator="DataTypeCheck" Type="Date" />
The key settings are Operator and Type.
Note if you have to consider multiple cultures and such, this becomes a much more involved question, hopefully that isn't the case.

Problem with MaskedEdit from Ajax Control Toolkit - different culture

I want to use maskededit to make the user to do the same as in the ajax control toolkit sample for maskededit, but I need to use another format, since dates at my client is displayed as dd-MM-yyyy. I also need to put a value in there to begin with, since the date is loaded from a db (once I get this code working, that is)
Look at this sample:
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/maskededit/maskededit.aspx
I need to do the same as in the textbox labeled "Enter Date (format: 99/99/9999): "
I downloaded the sample code and changed the format to "99-99-9999" in the relevant code:
But the sample date "12-12-1999" is not displayed in the textbox. Why not?
I cant figure out, what I am doing wrong. I also tried setting the cultureName to da-DK, but it did not chance anything. I also tried Chrome, Firefox and IE7, same behaviour...
How do I fix this?
<asp:TextBox ID="TextBox5" runat="server" Width="130px" MaxLength="1" style="text-align:justify" ValidationGroup="MKE">12-12-1999</asp:TextBox>
<asp:ImageButton ID="ImgBntCalc" runat="server" ImageUrl="~/images/Calendar_scheduleHS.png" CausesValidation="False" />
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender5" runat="server"
TargetControlID="TextBox5"
Mask="99-99-9999"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Date"
DisplayMoney="Left"
AcceptNegative="Left"
ErrorTooltipEnabled="True" />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server"
ControlExtender="MaskedEditExtender5"
ControlToValidate="TextBox5"
EmptyValueMessage="Date is required"
InvalidValueMessage="Date is invalid"
Display="Dynamic"
TooltipMessage="Input a date"
EmptyValueBlurredText="*"
InvalidValueBlurredMessage="*"
ValidationGroup="MKE" />
I suposse you'll have this already fixed, but ... have you tried using UserDateFormat="DayMonthYear" ?
UserDateFormat="DayMonthYear"
<asp:TextBox ID="TextBox5" runat="server" Width="130px" MaxLength="1" style="text-align:justify" ValidationGroup="MKE">12-12-1999</asp:TextBox>
<asp:ImageButton ID="ImgBntCalc" runat="server" ImageUrl="~/images/Calendar_scheduleHS.png" CausesValidation="False" />
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender5" runat="server"
TargetControlID="TextBox5"
Mask="99-99-9999"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Date"
DisplayMoney="Left"
AcceptNegative="Left"
ErrorTooltipEnabled="True" **UserDateFormat="DayMonthYear"** />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server"
ControlExtender="MaskedEditExtender5"
ControlToValidate="TextBox5"
EmptyValueMessage="Date is required"
InvalidValueMessage="Date is invalid"
Display="Dynamic"
TooltipMessage="Input a date"
EmptyValueBlurredText="*"
InvalidValueBlurredMessage="*"
ValidationGroup="MKE" />
I use ValidationExpression in MaskedEditValidator replace userDateFormat type Date dd/MM/yyy is very good.
This is :
ValidationExpression="(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))"
And in MaskedEditExtender put MaskType="none".
Validate ok.
<asp:TextBox ID="TextBox5" runat="server" Width="130px" MaxLength="10" style="text-align:justify" ValidationGroup="MKE" Text="12-12-1999"></asp:TextBox>
I am wondering if you move the text to the actual text attribute, if that would make a difference... I don't have the most recent version of AjaxToolkit other wise I would try it my self... Oh I just noticed is your max length really set to one or is that a copy and paste error? That would cause problems too.

Resources