C# password TextBox in a ASP.net website - c#

I'm trying to create a ASP .NET website that masks the password in a registration page as the user types. I found sites that use windows form but it doesn't work for a ASP .NET website.
So if the user types in a password its masked like this
*******
Any website or suggestion on how I can get it to work would be great.

To do it the ASP.NET way:
<asp:TextBox ID="txtBox1" TextMode="Password" runat="server" />

//in aspx page
<asp:TextBox ID="password" runat="server" TextMode="Password" />
//in MVC cshtml
#Html.Password("password", "", new { id = "password", Textmode = "Password" })

Use the password input type.
<input type="password" name="password" />
Here is a simple demo http://jsfiddle.net/cPaEN/

#JohnHartsock: You can also write type="password". It's acceptable in aspx.
<asp:TextBox ID="txtBox" type="password" runat="server"/>

I think this is what you are looking for
<asp:TextBox ID="txbPass" runat="server" TextMode="Password"></asp:TextBox>

Simply select texbox property 'TextMode' and select password...
<asp:TextBox ID="TextBox1" TextMode="Password" runat="server" />

Related

How to read user input from .aspx and store it in SQL Server database in .aspx.cs

I'm new to programming in asp.net, I'm still learning and right now I'm building a login form.
This is my aspx (HTML file):
<div class="login">
<input type="text" placeholder="User" name="user" id="user" runat="server"><br>
<input type="password" placeholder="Password" name="password" id="password" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click">Login</button>
</div>
I need to create a button click event in aspx.cs to store the data in SQL Server, in my registration form I did this:
cmd.Parameters.AddWithValue("user", user.Text);
cmd.Parameters.AddWithValue("password", password.Text);
The difference was that the TAG in the .aspx file for registration was like this:
<td class="auto-style8">
<asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>
</td>
How can I do the same that I did in the registration form without using these asp tags?
You are trying to get rid of ASP.NET tag controls which effectively are syntactic sugar around HTML equivalents. You could re-write you ASP Textbox tag as below:
<input type="text" ID="nome" runat="server" style="width:159px"></input>
The runat="server" attribute gives you the flexibility to access this control at code behind (aspx.cs) and if you do not intend to do that, it can be removed as well.
from code behind access the value of text control like this -
string userName = user.Value;
you can directly access value of controls by using name of that control
In your case
<td class="auto-style8">
<asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>
</td>
you can access this value in .cs file like
cmd.Parameters.AddWithValue("user", nome.Text);

prevent The Client side validation go to server side

I have a form and I have a validation in JavaScript. How to prevent the submit button go the the server side if the form is not valid?
<button id="LoginButton" onclick="Login.initReuiredValidation();"
onserverclick="LoginButton_Click" runat="server" type="submit"
class="submit btn btn-primary pull-right">
<asp:Literal runat="server" meta:resourcekey="LoginButton" />
<i class="icon-angle-right"></i>
</button>
in jquery you can do as
$(yourform).submit(function(event){
event.preventDefault();
//do somehting
})
This isn't classic asp, it's asp.net webforms and I've edited the tags accordingly
Adding input validation in webforms is quite easy, you can assign a validation control to each form control, eg
Contact Name<br />
<asp:TextBox ID="Contact_name" runat="server" Width="246 pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactNameValidator" runat="server"
ErrorMessage="Please enter your name" ControlToValidate="Contact_name"></asp:RequiredFieldValidator><br />
<br />
Contact Telephone<br />
<asp:TextBox ID="Contact_phone" runat="server" Width="246pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactPhoneValidator" runat="server" ControlToValidate="Contact_phone"
ErrorMessage="Please enter your telephone number"></asp:RequiredFieldValidator>
If you view the output, you will see that the validation is actually done with (client side) JavaScript. Asp.net generates that for you, you don't have to write it yourself.
Further information here
https://msdn.microsoft.com/library/a0z2h4sw%28v=vs.100%29.aspx
In the code behind add...
LoginButton.Attributes.Add("onclick", "return false;");

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

Password control is not being sent to server

I am trying to make a user register for my project and I want to take user's password with input(password). I used textboxes for name and surname and I can save them to database but I can't get user's input which has entered in input(password). Here is my code which I am using to save users in database.
TermProjectEntities entity = new TermProjectEntities();
protected void Button1_Click(object sender, EventArgs e)
{
Customer cust = new Customer
{
Name = txtName.Text,
Surname = txtSurname.Text,
Email = txtEmail.Text,
};
}
I can take textboxes variables with "textboxe's id.Text" but it's not working for input(password). Thanks for the help and sorry if it's an easy question and took your time.
Here is the HTML:
<form id="form1" runat="server">
<div>
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
Surname:
<asp:TextBox ID="txtSurname" runat="server"></asp:TextBox><br />
Password:
<input id="txtPassword" type="password" /><br />
E-Mail:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" /> <br />
</div>
</form>
I have copied your HTML from your comment onto the question to make it easier for people to answer. But while I was there I saw the problem is your password is not set to run on the server (therefore will not be sent back when you click the button).
So you currently have this:
<input id="txtPassword" type="password" /><br />
But it should be this:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br />
So, there are two main points:
Use asp:TextBox and not input to leverage ASP .NET Framework
Ensure every control you want to use contains runat="server"
This is because ASP .NET is Server-Side so you need to get your controls back to the server before they can be accessed and this is what the runat="server" attribute achieves.
You can then use attributes like TextMode="Password" to control how items are shown/act.

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