Issue in password field validation asp.net C# - c#

I have to make validation for password. It works fine but if i add runat=server my password validation does not work.And when i remove runat server it works fine.
my code is as:
<input value="" runat="server" class="validate[required] text-input" type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" name="password" id="password" />
<input value="" runat="server" class="validate[required,equals[password]] text-input" type="password" name="password2" id="password2" />
Any help on this topic will be appreciated.

You have taken normal HTML tag telling to act as server side control i,e runat="Server".Normal html control will not work as server side control,So please take asp.net controls and write required code.
<asp:TextBox runat="server"/>

when you put space in control properties asp create diffrent properties for element
in your textbox control you are creating properties like
required pattern
So, ASP Create your text box html like this
<input name="password" type="password" id="password" required="" pattern="asdsa" class="validate[required] text-input" />
Answer
So you need to create properties using code behind
you can Add control attribute at code behind if you having problem in ASP page
<input value="" runat="server" class="validate[required] text-input" type="password" name="password" id="password" />
at code behind
protected void Page_Load(object sender, EventArgs e)
{
password.Attributes.Add("required pattern","(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,}");
}

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);

Input submit onclick and onserverclick not working

I'm working on a simple login screen with the below code:
<form id="login">
<div class="formHeader">
<h1>Login</h1>
</div>
<div class="formDiv">
<input type="text" placeholder="Email" name="txtEmail"/>
<div class="inputImage fa fa-user"></div>
</div>
<div class="formDiv">
<input type="password" placeholder="Password" name="txtPassword"/>
<div class="inputImage fa fa-lock"></div>
</div>
<div class="formDiv">
<label id="remember">
<input type="checkbox" name="cbxRemberMe" value="Checked"/>
<div class="checkbox"></div><span>Remember me</span>
</label>
</div>
<div class="formDiv">
<input type="submit" value="LOGIN" onclick="btnLogin_Click" />
</div>
<div class="formFooter"><a class="forgot" href="#">Forgot Password</a></div>
and back-end code
protected void btnLogin_Click(object sender, EventArgs e)
{
}
But the button does not call the backend code at all.
Nothing happens except the URL changes from
"http://localhost:15726/Pages/Login.aspx" to
"http://localhost:15726/Pages/Login.aspx?txtEmail=&txtPassword=&ctl00=LOGIN"
I cannot put a Runat=Server attribute in the form as I have 2 forms on this page (login and register)
any help is appreciated.
What I have tried:
Playing around with Runat=Server, onclick() and OnServerClick() (swapping around and changing them up)
Making input type=submit / input type=button
none of these work.
Have you tried these two approaches?
<asp:Button runat="server" id="btnLogin" Text="LOGIN" OnClick="btnLogin_Click">
or
< button onserverclick="btnLogin_Click" runat="server" id="btnLogin">LOGIN</button>
Edit
This was supposed to be a comment and not answer. Sorry for that.
However, the entire form doesn't seem to be "webform" form compatible. If you want to be able to read the values from the form elements the form should have a runat="server" tag, too.
Firstly, you need to specify whether it is a server control or client control. Specify runat=server to tell that it should be available for the Code-Behind file.
Finally, Use any javascript function with OnClick and any code behind function with OnServerClick
Something like this,
aspx
Plain HTML
<input type="submit" runat="server" value="LOGIN" onclick="return js_btnLogin_Click()" OnServerClick="btnLogin_Click"/>
or with ASPX Control
<asp:Button runat="server" Text="LOGIN" OnClientClick="return js_btnLogin_Click()" OnClick="btnLogin_Click">
javascript
<script type="text/javascript">
function js_btnLogin_Click()
{
//return false, in case if you want to stop posting the form to the server
return true;
}
</script>
Code-Behind
protected void btnLogin_Click(object sender, EventArgs e)
{
}

Custom postback inside aspx and redirect

I need to perform a postback in order to process a gateway payment by redirect, so i've got an example in order to do inside an html with server side code (runat=server in the inputs):
<form action="https://xxxxxxxx" method="post">
<input runat="server" type="text" id="param1" name="param1" value="asd" />
<input runat="server" size="100" type="text" id="parm2" name="parm2" value="asd" />
<input runat="server" type="text" size="50" id="param3" name="param3" value="asd" />
<input runat="server" type="submit" value="Post!" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
// New instance of library and his magic
LibraryXXX r = new LibraryXXX();
r.Parameter.Add("xxxx", xxxx);
// more logic goes here
}
Clinking the submit button sends the postback and the page loads an html. I want traslate that sample to an aspx webform. The problem is i can't have more than one form label inside an aspx. So, how can i do the same postback inside an aspx?

If condition show error for radio button id

I have following input tags inside table in asp.net
<td class="style18">
<asp:CheckBox ID="chkContentTone" onclick="enableGroupBoxTone()"
runat="server" Text="Content Tone" CssClass="ChkBoxStyle"/>
</td>
<td class="styleCntTon">
<input type="radio" id="rdBtnPositive" name="q2" title="Posotivetitle" value="positive"/>
<label for="rdbtnPositive" class="RadioGroup">Positive</label>
<input type="radio" id="rdBtnNegative" name="q2" title="Negativetitle" value="negative"/>
<label for="rdBtnNegative" class="RadioGroup">Negative</label>
<input type="radio" id="rdBtnNeutral" name="q2" title="Neutraltitle" value="neutral"/>
<label for="rdBtnNeutral" class="RadioGroup">Neutral</label>
</td>
but when I put an if condition in, the radio button's the show an error.
Any ideas why this is occurring?
Try to use Asp.net controls:
<asp:RadioButton runat="server" ... />
The markup you posted does not include server side controls hence those controls cannot be accessed via server side code.
Looking at what you want seems like you would need an asp:RadioButton
once you add a asp:radiobutton you can access these controls in your server side code.
Change this:
<input type="radio" id="rdBtnPositive" name="q2" title="Posotivetitle" value="positive"/><label for="rdbtnPositive" class="RadioGroup">Positive</label>
To this:
<asp:RadioButton id="rdBtnPositive" Label="Positive" runat="server" />
Then you can do:
if(rdBtnPositive.Checked)
{
//code it...
}
You'll need to do that for all three radiobutton's, each should have a unique id and be a server side control, namely asp:RadioButton.
you should use asp:RadioButton to be able to access in code behind.
So basically replace all "input" tags with "asp:RadioButton" and remove non sensible properties like Type.
asp:RadioButton usage example

Getting data from an Input control in to code behind

I am using asp.net C#
I am also using the jQuery UI Calendar control.
calendar control
It appears the calendar control wants to work with an input control with an ID of "datepicker".
<input type="text" id="datepicker" />
I want to use value of the input control in my code behind but seeing how it is not an asp.net control I am not sure how I can reference it in the code behind.
Does anyone know how to get the value of the input control in the code behind?
use
<input type="text" id="datepicker" name="datepicker" />
and in the code behind:
Request.Form["datepicker"]
In fact, Form property of Request is populated with form values.
Here you have to mention your form name which contains input tag
<input name="txtemail" id="txtemail" runat="server" type="text" class="cssspa" form="form"/>
in C#
string email = txtemail.Value.ToString();
now u can get data from textbox
If you want to use basic form pulling, you need to add a name attribute:
<input type="text" id="datepicker" name="datepicker" />
if(Page.IsPostBack)
{
string value = Request.Form["datepicker"];
}
Alternatively, you can mark is as an HtmlInputControl
<input type="text" id="datepicker" runat="server" />
System.Web.UI.HtmlControls.HtmlInputControl datepickerControl = this.FindControl("datepicker") as System.Web.UI.HtmlControls.HtmlInputControl;
if(datepickerControl != null)
string value = datepickerControl.Value;
You can access the value via the Request object like onof wrote, or you can turn every html tag into a server control by adding the attribute runat:
<input type="text" id="datepicker" runat="server" ClientIdMode="static" />
The attribute ClientIdMode="static" ensures that the tags ID is not changed by the ASP.NET runtime.
Then you can access the input by the autogenerated member datePicker.
Since you explicitly say code-behind, I assume you probably want to stay with the WebForms/Controls model? With clientidmode you can do that while also having a determinite id value for the calendar.
<asp:TextBox id="datepicker" ClientIDMode="Static" runat="server" />
Then you can interact directly with the control from the code-behind as normal.
But, the calendar control does not require a specific ID. You can initialize it with whatever ID you want
<asp:TextBox id="DateTextBox" runat="server" />
<script>
$(function() {
$( "#<%=DateTextBox.ClientID%>" ).datepicker();
});
</script>

Categories