I am using asp.net RequiredFieldValidator while it is failing and showing the correct fields what I want to do is change the active control to red i.e not display just a message so the user knows how which control and see it better
<asp:ValidationSummary ID="validationSummary" runat="server" ValidationGroup="fhsMain" ForeColor="Red" HeaderText="Please ensure values are in the following fields" />
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">
First Name</label>
<div class="col-md-8">
<telerik:RadTextBox ID="txtFirstName" CssClass="form-control" Width="60%" Skin="Bootstrap" runat="server"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="fhsMain" runat="server" ControlToValidate="txtFirstName" ErrorMessage="First Name"></asp:RequiredFieldValidator>
</div>
</div>
Based upon what you indicate as your goal I believe you should add the Text attribute to your validator (see the last line in the example below). This is separate from the ErrorMessage which renders in your ValidationSummary. When the validator fails, the text will display where you put the validator and the errormessage will display in your summary.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="fhsMain"
runat="server" ControlToValidate="txtFirstName" ErrorMessage="First Name"
Text="Text To Display"></asp:RequiredFieldValidator>
If you really want the input itself to change colors then you're going to need to implement some custom Javascript or utilize an external library like formvalidation.io.
Related
When I am using ValidationSummary to validate my page if I have duplicate errors my validation will show all this errors. I want to display a distinct list of errors. I think that the best approach is to ovverride an event. But I don't know what event to override. What is the event that deals with showing errors.
I don't want solutions for MVC projects!
ValidationSummary collect all the error in your input and display.
So indirectly you have answerd your question by yourself in your question
You just don't know the syntax i think.Here it is:
If you have some collection of input in aspx,you have defined also the regular expression for specific input.For example:
<div>
<asp:TextBox ID="txt" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt" ControlToValidate="txt" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:TextBox ID="txt1" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt1" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt1" ControlToValidate="txtEmail" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Save" ValidationGroup="Submit" OnClick="btnSubmit_Click" />
</div>
<div>
<asp:ValidationSummary ID="ValidationSummary"runat="server"ValidationGroup="Submit" />
</div>
So in every input or button you have to define ValidationGroup attribute
Else if you want to check in codebehind if all of this input are validated you have to do this:
if(Page.IsValid)
{
//your code here
}
It seems like if I use TextMode="password"it changes the size of the textbox and also the placeholder text style. It doesn't match the other textboxes that I made. Although it does the work for me, just curious if there is another way to make it happen to avoid ruining the design. Thanks!
<div class="form-group" style= "float:left">
<asp:TextBox ID="txtPW" runat="server" name="form-password-name" Width="200px"
placeholder="Password..." class="form-password form-control"
TextMode="Password"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server"
ErrorMessage="Only letters and numbers are allowed" Display="Dynamic" ControlToValidate="txtPW"
ForeColor="Red" ValidationExpression="^[a-zA-Z0-9]+$">
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="Password is required." ControlToValidate="txtPW" Display="Dynamic" ForeColor="Red">
</asp:RequiredFieldValidator>
</div>
you can use a regular html textbox:
<input type="password" name="form-password-name" id="txtPW" class="form-password form-control"/>
So I've been looking all over and can't seem to find a similar problem.
Basically, it seems like using CompareValidator doesn't work without a RequiredFieldValidator.
<div class="control-group">
<label class="control-label" for="PositionName">
Password:</label>
<div class="controls">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<%--<asp:RequiredFieldValidator ID="rvPassword" runat="server" ControlToValidate="txtPassword"
ErrorMessage="Please Enter Password" SetFocusOnError="True" ValidationGroup="1"
CssClass="error"></asp:RequiredFieldValidator>--%>
</div>
</div>
<div class="control-group">
<label class="control-label" for="PositionName">
Confirm Password:</label>
<div class="controls">
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqConPass" runat="server" ControlToValidate="txtConfirmPassword"
ErrorMessage="Please Enter Confirm Password" SetFocusOnError="True" ValidationGroup="1"
CssClass="error"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="compPassword" runat="server" ControlToValidate="txtConfirmPassword"
ControlToCompare="txtPassword" ErrorMessage="Password Mismatch" SetFocusOnError="True"
ValidationGroup="1" CssClass="error"></asp:CompareValidator>
</div>
</div>
Basically, you can see I have the RequiredFieldValidator commented out for both pass and confirm pass. When I do this, I can submit with only a value in the txtPassword textbox and nothing in the txtConfirmPassword textbox.
If I uncomment the RequiredFieldValidators then it compares as it should.
If it helps, the reason I need to do this is because I am unable to decrypt the password and autofill the textbox with their current password. So whenever a user is editted, they will need to enter a new password everytime with a RequiredFieldValidator on it.
So my solution was to get rid of the RequiredFieldValidator and just check if the text is null or empty, and if it is, don't update the password, but if it isn't then update the user without updating the password.
I hope this makes sense, and if anyone can help I would greatly appreciate it.
If you need more info please ask.
Thanks again!
See this code snippet, in this first for password i have used Regular expression validator and once password is valid then i have enabled compare validator.
<script>
function Validate() {
if (document.getElementById('<%=txtPassword.ClientID %>').value != "")
ValidatorEnable(document.getElementById('<%=ConfirmPasswordRequired.ClientID %>'), true);
else
ValidatorEnable(document.getElementById('<%=ConfirmPasswordRequired.ClientID %>'), false);
}
</script>
<p>
Password
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RegularExpressionValidator ID="PasswordRegularExpression" runat="server"
ErrorMessage="*Password must be at least 8 characters long and include at least one Special Character, one Number, and one Capital letter."
ValidationGroup="ValidationGroup1" ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).*$"
ControlToValidate="txtPassword" >
</asp:RegularExpressionValidator>
</p>
<p>
Confirm Password:
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="txtConfirmPassword"
ErrorMessage="*Confirm Password is required."
Enabled="false" ValidationGroup="ValidationGroup1"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="NewPasswordCompare" runat="server" ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword" ErrorMessage="*Confirm Password must match with the Password."
ValidationGroup="ValidationGroup1"></asp:CompareValidator>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Save" ValidationGroup="ValidationGroup1"
OnClick="Button1_Click" OnClientClick="Validate();" />
</p>
Here's one thought, I also ended up using this solution:
How about setting the compare validator to validate the password textbox and compare it to the confirmation.
This way, the compare validator only fires if there is a value inside the password textbox.
<div class="control-group">
<label class="control-label" for="PositionName">Password:</label>
<div class="controls">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="PositionName">Confirm Password:</label>
<div class="controls">
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"/>
<asp:CompareValidator ID="compPassword" runat="server" ControlToValidate="txtPassword"
ControlToCompare="txtConfirmPassword" ErrorMessage="Password Mismatch" SetFocusOnError="True"
ValidationGroup="1" CssClass="error"/>
</div>
</div>
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!" />
hi my dear friends :
i have a button with CausesValidation Property to false...
it's code like below :
<telerik:RadButton ID="RadbtnViewImage" runat="server" Text="view" CausesValidation="False" EnableEmbeddedSkins="False" Skin="BlackByMe" Font-Names="Tahoma" Width="80px" onclick="RadbtnViewImage_Click">
</telerik:RadButton>
also i have a RadComboBox with a CostumValidator that is in relationship with that RadComboBox...
it's code like below :
<div class="EditRow">
<div class="RightEditColumn">
imageGroupName
</div>
<div class="LeftEditColumn">
<telerik:RadComboBox ID="RadcbImageGroupInrpvEdit" runat="server" DataSourceID="sdsImagesGroup"
DataTextField="Title" DataValueField="ID" EnableEmbeddedSkins="False" Skin="BlackByMe2"
AppendDataBoundItems="True" MarkFirstMatch="True" LoadingMessage="loading...."
CausesValidation="False" ValidationGroup="B">
<Items>
<telerik:RadComboBoxItem runat="server" Text="plz select one" Value="0" />
</Items>
</telerik:RadComboBox>
</div>
<div class="ValDivInrpvEdit">
<div style="display: inline;">
<span id="spnOfcvImageGroupInrpvEdit" class="ttTarget">
<asp:CustomValidator ID="cvImageGroupInrpvEdit" runat="server" ControlToValidate="RadcbImageGroupInrpvEdit"
Display="Dynamic" ValidationGroup="B"
onservervalidate="cvImageGroupInrpvEdit_ServerValidate">
<span class="imgValContainerInrpvEdit">
<asp:Image ID="img4cvImageGroupInrpvEdit" CssClass="imgValidateInrpvEdit" runat="server" AlternateText="attention"
ImageUrl="~/Images/Exclamation.png" /></span>
</asp:CustomValidator>
</span>
<div id="tt44cvImageGroupInrpvEdit" class="ttContent">
plz choose
</div>
</div>
</div>
</div>
when the button is clicked i just want to check only that RadCombobox Validation and show the CustomValidator Text of it to users / not the other validators !
how can i do that ?
(the other controls In my page plus that RadComboBox have a same ValidationGroup -> Mean "B")
thanks in advance
Attach a second validator for this scenario with its own distinct validation group. This way, you can fire this one validator for this specific scenario, and use the original validator to validate when the entire form is processed. Depending on how you need to validate, you can use Required (set InitialValue to the initial value of the combo), or Custom.
HTH.