Error when using RequiredFieldValidator control for FreeTextBox control - c#

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";
}

Related

adding TextMode attribute to a textbox results in removal of all styles from the textbox

Hello guys I am making a login form where i want the txtpassword textbox to have
TextMode="password" but as soon as I add this attribute all my styles are gone I want to know how can i hide the password (instead show dot like characters) and also have styles applied here is my code if you guys need
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
<%--this runs perfectly--%>
<br />
<asp:TextBox ID="txtpassword" TextMode="Password" runat="server"></asp:TextBox>
<%--styles aren't there as soon as i add TextMode or type attributes--%>

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.

ASP compareValidator's error message won't disappear after a valid input in entered

I have a form for adding some values to my DB. One of the input requires a Integer value, so on that TextBox I have two validators, an RequiredFieldValidator and an CompareValidator. The problem is that when I click on the input the first time ( or I got there with tab) the error message is displayed an it will never disappear, even if I enter a valid input.
<asp:Label ID="label4" runat="server" Text="label4"></asp:Label>
<asp:TextBox ID="textBox4" runat="server" style="width: 170px; margin: 5px 0;"></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredFieldValidator4" runat="server"
ErrorMessage="*" ControlToValidate="textBox4" Display="Dynamic"
ForeColor="Red" ValidationGroup="1"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="compareValidator4" runat="server"
ErrorMessage="*" ControlToValidate="textBox4"
Type="Integer" Operator="DataTypeCheck" Display="Dynamic"
ForeColor="Red" ValidationGroup="1">
</asp:CompareValidator>
Above is my code for that input.
Have you already tried adding the property "ControlToCompare" to your compare validator?, for reference take a look of this post: http://forums.asp.net/t/1842937.aspx?CompareValidator+doesn+t+disappear+when+entering+the+correct+value

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.

can't get an asp:RegularExpressionValidator to work

I'm having an issue getting a regex field validator to work for an asp page i'm trying to update.
Here is the asp:Panel stripped down to the important bits:
<asp:Panel ID="pnlEmailAddressCollection" runat="server">
<div id="POMInput-wrapper">
<div class="POMInput-FieldText">
<span class="POMInput-wrapper-text">Name:</span>
<br />
<span class="POMInput-wrapper-text">Email Address:</span>
<br />
</div>
<div class="POMInput-FieldEntry">
<asp:TextBox ID="txtEmailAddress" name="emailAddress" runat="server" CssClass="textInput"></asp:TextBox>
<asp:TextBox ID="txtUserName" runat="server" name="firstName" CssClass="textInput"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="rfvNameValidator" runat="server"
ErrorMessage="Please enter your name"
ControlToValidate="txtUserName"
Display="None" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please enter your email address"
ControlToValidate="txtEmailAddress"
Display="None" />
<asp:RegularExpressionValidator ID="rfvEmailValidator2" runat="server"
ErrorMessage="Please enter a valid email address"
ControlToValidate="txtEmailAddress"
Display="None"
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="true"
ShowSummary="false"
EnableClientScript="true" />
</div>
</asp:Panel>
It is currently failing on any email i put in. The asp:RequiredFieldValidator's work as expected.
I tested the regular expression in a test project and the regex seems good (returns true on valid emails, false on invalid ones). Did I set up the asp:RegularExpressionValidator incorrectly?
You should remove double backslash:
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
Note that you put two backslashes where you meant only one. If you were to set this expression from code behind, the string you provided is correct. But in aspx you don't have to escape backslash.
At the moment accepted email address would be something like abc#abc{backslash}.com
You can try with this code
ValidationExpression="[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
Nota : you can delete ^ and $ symbols
The regular expression works within .NET (server-side), but is failing due to the client-side JScript implemetation as documented in the Remarks section. To verify this (it passes server-side validation), set the EnableClientScript property on the validator to false.
Then undo that change and verify the regex will pass on the client side. You can use an online tester, if it's easier for you.

Categories