CompareValidator without RequiredFieldValidator? - c#

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>

Related

How can I make the textbox control show red as well

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.

Is there other way to hide the password in textbox (***) aside from textboxmode="password"?

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"/>

Asp.net get value from Textbox in aspx to code behind

I'm creating a login system in asp.net and C# programming language. The code behind to handle the user and password is done. But in view layer, I'm troubling to get the values from username textbox and password textbox and passing it to codebehind.
Both textboxes are ID identified and in my few skills of programming, an ID should be enough to access the elements.
This is my aspx login page:
<asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
<LayoutTemplate>
<p class="validation-summary-errors">
<asp:Literal runat="server" ID="FailureText" />
</p>
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
<asp:Label ID="Label1" runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
</li>
<li>
<asp:Label ID="Label2" runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
</li>
<li>
<asp:CheckBox runat="server" ID="RememberMe" />
<asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
</li>
</ol>
<asp:Button ID="Button1" runat="server" CommandName="Login" Text="Log in" OnClick="Button1_Click"/>
</fieldset>
</LayoutTemplate>
</asp:Login>
This I did do get values from UserName and Password Textboxes:
Using the code:
string user = this.UserName.Text;
string pass = this.Password.Text;
Using the code:
Textbox UserName = this.FindControl("UserName");
Deleted the aspx.design.cs and right click on the form and Convert it to application;
In the designer, add the following lines of code:
protected global::System.Web.UI.WebControls.TextBox UserName;
protected global::System.Web.UI.WebControls.TextBox Password;
Nothing worked so far, and when I reach this line:
string user = this.UserName.Text;
It throws me an error:
Object Reference not set an instance of an object.
Can you suggest any solution to my problem?
This is because these controls are parts of a template. They are not directly on the page, they are added there dynamically when Login control is initialized. To access them you need FindControl:
string user = ((TextBox)Login1.FindControl("UserName")).Text;

Is the Login control buggy?

From the documentation here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.layouttemplate.aspx i had customized the user interface of login control through layout template like below
<LayoutTemplate>
<h3 class="login-title page-header">
<asp:Literal runat="server" ID="loginHeading" Text="Sign-in"></asp:Literal>
</h3>
<div class="clearfix">
<asp:Label runat="server" ID="lblUserName" Text="Username" AssociatedControlID="UserName"></asp:Label>
<div class="input">
<asp:TextBox runat="server" ID="UserName"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvTxtUserName" ErrorMessage="Username is required"
ControlToValidate="UserName" Text="Username is required"></asp:RequiredFieldValidator>
</div>
</div>
<div class="clearfix">
<asp:Label runat="server" Text="Password" AssociatedControlID="Password"></asp:Label>
<div class="input">
<asp:TextBox runat="server" ID="Password" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvTxtPassword" ErrorMessage="Password is required"
ControlToValidate="Password" Text="Password is required"></asp:RequiredFieldValidator>
</div>
</div>
<div class="clearfix">
<asp:Label runat="server" Text="Remember Me" AssociatedControlID="RememberMe"></asp:Label>
<div class="input">
<input type="checkbox" runat="server" id="RememberMe" />
</div>
</div>
<div class="actions action-fix">
<asp:Button runat="server" ID="Login" Text="Login" CssClass="btn success small" />
<input type="reset" class="btn small" value="reset" />
</div>
</LayoutTemplate>
as you can see i did name the control ID as required by the control. Also i use CSS Friendly control adapter for rendering the Login control.
The Problem
It just happens after user types username and password and submits the details nothing happens the page just reloads causing a postback.
What am i doing wrong here
info:
more problem seems to be more prominent for users using LoginControlAdapter look here http://forums.asp.net/t/1043974.aspx/2/10
you must be missing CommandName paramter
<asp:button id="Login" CommandName="Login" runat="server" Text="Login"></asp:button>
Try adding command name to your login button;
<asp:Button runat="server" ID="Login" Text="Login" CssClass="btn success small" CommandName="Login" />

ASP.NET logon and registration with browser password memory

I have followed the asp.net pages for registration and login. Everything works, except that the username/password combo is never remember by the browser (ie. browser autocomplete). This is an issue with all browsers. The key fields seem to be named appropriately, I think:
<LayoutTemplate>
<div class="formlayout">
<p>
Enter your login details</p>
<asp:ValidationSummary runat="server" DisplayMode="BulletList" CssClass="errors" />
<div class="b">
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ErrorMessage="Invalid email address."
ValidationExpression="^[\w\.\-]+#[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$"
ControlToValidate="UserName" SetFocusOnError="false" >*</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="Enter your email address."
ControlToValidate="UserName" SetFocusOnError="false" >*</asp:RequiredFieldValidator>
Email address<div class="s">
Your login identity</div>
</div>
<div class="tb">
**<asp:TextBox runat="server" ID="UserName" />**</div>
<div class="b">
<asp:RequiredFieldValidator runat="server" Display="Dynamic"
ErrorMessage="Enter your password." ControlToValidate="UserName" SetFocusOnError="false" >*</asp:RequiredFieldValidator>
Password<div class="s">
Your registered password</div>
</div>
<div class="tb">
**<asp:TextBox runat="server" ID="Password" Name="Password" ClientIDMode="Static" TextMode="Password" />**</div>
<div class="shift">
<asp:CheckBox runat="server" ID="RememberMe" Text="Keep me logged in" />
</div>
<div class="shift">
<asp:Button ID="BtnLogin" ClientIDMode="Static" runat="server" CommandName="Login" Text="Login" />
</div>
</div>
</LayoutTemplate>
To clarify: My browser is set to remember passwords. Here's a thought... I am testing off 'localhost' -- are browsers set not to remember usernames and passwords that are running off localhost?
Your server-side code does not have anything to do with whether or not the browser's Auto-Complete works. That's a setting in the browser.

Categories