ASP.NET RequiredFieldValidator "Message" on Valid - c#

I am currently using ASP.NET to validate my forms and instead of giving an error message if the fields are not valid I am using icons to show a green checkmark or a red exclamation mark.
<div class="name-field">
<asp:TextBox ID="RealName" Name="user-name" placeholder="请输入姓名" runat="server" CssClass="user-name" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RealName" CssClass="name-reset" ErrorMessage="<img src='image/mobile/exclamation-mark.png'>" />
</div>
Right now on field not valid, I am using 'ErrorMessage=...' to set the error icon. What is the best way I can also set the valid icon if the user has entered everything correctly?
Thanks.

Ok You can try this
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ontextchanged="TextBox1_TextChanged">
your class code
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//eg: Label1.Text = Server.HtmlEncode(TextBox1.Text);
//you type your code here
}

you can try this
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Error" ControlToValidate="TextBox1"><img src="img src=image/mobile/exclamation-mark.png"/></asp:RequiredFieldValidator>

try to insert the image tag inside the RequiredFieldValidator tag like this:
<div class="name-field">
<asp:TextBox ID="RealName" Name="user-name" placeholder="请输入姓名" runat="server" CssClass="user-name" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RealName" CssClass="name-reset">
<img src="image/mobile/exclamation-mark.png" />
</asp:RequiredFieldValidator>

Related

Validation message error not working properly

I'm having a problem with validation message errors for web forms. I need to get an error when both statements are true(both fields are empty). When I change statement && for || I'm able to get an error but that's not what I want.Thank you. Here is my C# code
protected void CustomValidatorForm_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(drpState.Text) && string.IsNullOrEmpty(txtRegion.Text))
args.IsValid = false;
else
{
args.IsValid = true;
}
}
I'm Trying to run this code for my forms:
<asp:DropDownList ID="drpState" runat="server" CausesValidation="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
<asp:CustomValidator ID="CustomValidatorForm" runat="server"
ControlToValidate ="txtRegion" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
try using like this ,
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="*" ControlToValidate="txtFrom" runat="server" Display="Dynamic" ForeColor="Red" />
Easy way to do this,
After you create a <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
type <asp:RequiredFieldValidator and press Tab Button twice,
You can also give any ErrorMessage as well as define color of the ErrorMessage
It turns out I just needed to add this property ValidateEmptyText="True" and set it to true. This is how validator should look like:
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red" ValidateEmptyText="True">
</asp:CustomValidator>

findControl in Naming container (Asp.net webform C#)

I have a problem about findControl method in naming container.
It's not the first trouble about that and I would like understand the theory.
I found many solutions on website but nothing works
I have a DetailsView which contains controls.
I put DefaultMode "Insert" and I add 2 radio buttons
<asp:DetailsView ID="DetailsView1" runat="server"
ItemType="[...]"
DefaultMode="Insert"
[...]">
<Fields>
<asp:TemplateField>
<InsertItemTemplate>
<asp:Panel ID="Panel1" runat="server" GroupingText="Create or Select">
<div class="Select">
<asp:RadioButton ID="RB_Select" runat="server" Text="Select" Checked="True" AutoPostBack="true" OnCheckedChanged ="RB_Select_CheckedChanged" />
<asp:DropDownList runat="server" ID="DDL_Select"
ItemType="[...]"
[...]
AutoPostBack="true">
</asp:DropDownList>
</div>
<div class="New">
<asp:RadioButton ID="RB_New" runat="server" Text="New" Checked="false" AutoPostBack="true" OnCheckedChanged="RB_New_CheckedChanged" />
<asp:TextBox ID="TXB_New" runat="server" Enabled="false" Text="<%# BindItem.Label %>"></asp:TextBox>
</div>
</asp:Panel>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
And for exemple in my behind Code, I Just want to test if radiobutton is check or not :
protected void RB_New_CheckedChanged(object sender, EventArgs e)
{
var RadioButtonNew = (RadioButton)FindControl("RB_New");
var RadioButtonSelect = (RadioButton)FindControl("RB_Select");
RadioButtonSelect.Checked = !RadioButtonNew.Checked;
}
And I have a "System.NullReferenceException" because it doesn't find my controls.
Why it doesn't recognize my controls? And how to deal with this?
Thanks in advance
You are using FindControl on a Page level. But the Controls are inside a DetailsView, so you need to access that first.
TextBox tb = DetailsView1.FindControl("TXB_New") as TextBox;
//or
var RadioButtonSelect = (RadioButton)DetailsView1.FindControl("RB_Select");
Thank you again, I found the solution.
I didn't know but Panel element acted like a container.
I just add a findControl :
var RadioButtonSelect = (RadioButton)DetailsView1.FindControl("Panel1").FindControl("RB_Select");

Required attribute in ASP Control

I want to know how to use required attribute in asp.net. .In html I used required attribute for mandatory fields. How to use it in asp .net?
I tried this so far, is it correct?
<p class="field-wrapper required-field">
<label>First Name</label>
<asp:TextBox ID="TextBox1" name="f_name" runat="server" required>
</asp:TextBox>
</p>
there's no required attribute. you have to use a RequiredFieldValidator
<asp:TextBox ID="TextBox1" name="f_name" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="TextBox1" errormessage="Please enter a value!" />

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

CustomValidator message doesnt show up

I've a CustomValidator and I defined every possible parameter of it:
<asp:CustomValidator ID="custom" runat="server" Text="*" ErrorMessage="This email address is already registered" ControlToValidate="txtEmail" OnServerValidate="isExist" Display="None" ValidationGroup="valRegister"></asp:CustomValidator>
PS: I've a RequiredFieldValidator for same textbox and I dont want to check empty value.
Here are other objects of the form:
<div class="row"><asp:Label runat="server" Text="Email" AssociatedControlID="txtEmail"></asp:Label><asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter your email" Text="*" ControlToValidate="txtEmail"></asp:RequiredFieldValidator><asp:TextBox ID="txtEmail" runat="server" CssClass="inpBox"></asp:TextBox></div>
<asp:Button runat="server" Text="Register" CssClass="btn" OnClick="register_member" CausesValidation="true" ValidationGroup="valRegister" />
<asp:ValidationSummary ID="validationSummary" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="valRegister" />
protected void isExist(object sender, ServerValidateEventArgs args){
if (cre.member.isExist(args.Value)){
args.IsValid = false;
} else {
args.IsValid = true;
}
}
When I put an email already exist in the db table * appears on the form, but the error message doesnt show up. I tried all display options for custom error but no luck.
I took the code exactly as in your question.
Changing Display="None" to Display="Dynamic" in the asp:CustomValidator causes the asterisk to appear.
Changing ShowSummary="false" to ShowSummary="true" in the asp:ValidationSummary causes the error message to appear in the summary.
Changing the Display to "Dynamic" or anything doesn't really do anything if the server is not handling the validation manually, especially when using <asp:CustomValidator. Even a ValidationGroup with or without a ValidationSummary does nothing.
Always force a validation on the server before allowing the user to exit the form/gridview/etc.
ie
...your form here...
<tr>
<td colspan="3" style="text-align: center" valign="top">
<asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="Submit_Click" CausesValidation="true" />
<asp:Button ID="ButtonCancel" runat="server" Text="Cancel" OnClick="Cancel_Click" CausesValidation="false" />
</td>
</tr>
</table>
</asp:Panel>
...
protected void Submit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
//processing done after a successful submit here!
}
}
The Page.Validate() will force the validation controls to check and display your error message.

Categories