textbox password gets shown on alert message - c#

My textbox HTML code is:
<asp:TextBox ID="txt_pass"
runat="server" class="txt-box_reg"
onfocus="if (this.value == 'Password')
{
this.value = '';this.type='password';
}"
onblur="if (this.value == '')
{
this.value = 'Password';this.type='text';
}"
value="Password">
</asp:TextBox>
and my javascript code is:
<script type="text/javascript" language="javascript">
function myFocus(element) {
if (element.value == element.defaultValue) {
element.value = '';
}
}
function myBlur(element) {
if (element.value == '') {
element.value = element.defaultValue;
}
}
</script>
All things work fine till the alert message box opens on my page.
When the alert message opens, the password shows and when I close the alert box the password regain its value as...
//More INFORMATION//
When I put textmode="password" then the second textbox shows but I use to show my textbox as 3rd textbox i.e. Confirm Password

Do one thing, try it in your source code.
if(your alert box condition)
{
//your alert box coding
txt_pass.Text = ""; // or make text as you wish.
}
else.....
do this in your .cs page, at where you popup your Alert-box.

You are able to it using jQuery:
$(".txt-box_reg").focus(function (pas) {
$(".txt-box_reg").attr("Type", "Password");
});

Related

Show a div tag content on entering a character into a textbox?

I am trying to slide down content within a div tag (red rectangle) when a user enters atleast one character in a textbox (green rectangle).
If no character is entered then the div content does not show.
Here is a pic of the aspx (design):
And this is the JavaScript I am using to try and achieve this:
function() {
$("#SubmitSection").hide();
inputOne = document.getElementById("<%= TextBox1.ClientID %>");
if (inputOne.value != "") {
$("#SubmitSection").slideDown('slow');
}
else {
$("#SubmitSection").slideUp('slow');
}
}​
All the backend code works just fine but both the textbox and the div section show up when deployed.
if i got you right :
$("<%= TextBox1.ClientID %>").change(function(){YoursSlidingFunctionhere();})
.change()
You have to hook up to an event on the textbox. The onchange event only fires after the textbox loses focus, so I suggest the onkeyup event.
<asp:TextBox ID="TextBox1" runat="server" [...] onkeyup="updateSubmitSection()" />
You will have to name your function so that you can call it:
updateSubmitSection = function() {
$("#SubmitSection").hide();
inputOne = document.getElementById("<%= TextBox1.ClientID %>");
// ...
}
In addition to that, you need to call the function during the onload event of the page:
<body onload='updateSubmitSection()'>
The same solution using jQuery:
$(document).ready(function() {
updateSubmitSection();
$('#<%= TextBox1.ClientID %>').change(updateSubmitQuery);
}
You'll need to add this to a section of your javascript that is executed on initialization:
`$("#SubmitSection").hide();`
You could use the body onload attribute:
`<body onload="init()">
<script>
function init()
{
$("#SubmitSection").hide();
}
</script> `

How to check block adding characters if the input text is a special character?

I have a gridview and it has some columns .
For example: Name|Age|Birthday
How can I filter the textbox so if the user is typing a special character it won't be added.
I know I need to use this : ^[^0-9a-zA-Z]*$ , but I don't know how.
Thanks
OnBlur and OnKeyUp Events of TextBox
JavaScript
<script language="javascript" type="text/javascript">
function validation() {
var txt = document.getElementById('<%= txt.ClientID%>').value;
var regex = new RegExp('[^0-9a-zA-Z]+');
while (txt.match(regex)) {
if (txt.match(regex)[0] == "") break;
txt = txt.replace(regex, '');
}
document.getElementById('<%= txt.ClientID%>').value = txt;
}
function onkeyUpEvent() {
validation();
}
function onBlurEvent() {
validation();
}
</script>
MarkUp
<asp:TextBox ID="txt" runat="server" onkeyup="onkeyUpEvent();"
onblur="onBlurEvent();"></asp:TextBox>
var lastValidValue;
input.addEventListener('keydown', function(evt) {
// before change capture current value;
lastValidValue = input.value;
}, false);
function onchange(evt) {
if (/^[^0-9a-zA-Z]*$/.test(input.value))
// if its valid, update preserved field.
lastValidValue = input.value;
else
// otherwise revert to previous data.
input.value = lastValidValue;
}
input.addEventLisenter('keyup', onchange, false);
input.addEventListener('change', onchange, false);
where input is a reference to your textbox.

How to handle Yes No dialog that was pop-up using Javascript on button click

I am having an update button on my from on clicking update i would like to prompt the user as Do you want to delimit the record with Yes and No buttons. If the user clicks on Yes i would like to execute the code which can delimit the record if not just update the record.
My sample code
protected void btnUpdate1_Click(object sender, EventArgs e)
{
EmpID = Convert.ToInt32(HiddenField1.Value);
if (ID == 2)
{
oEmployeePersonalData.EmpID = EmpID;
oEmployeePersonalData.PhoneNumberTypeID = ddlPhoneType.SelectedValue;
oEmployeePersonalData.PhoneNumber = Convert.ToInt64(txtph1.Text);
oEmployeePersonalData.EndDate = DateTime.Today.AddDays(-1);
//As per my requirement if i click on yes i would like to execute this code
if (oEmployeePersonalData.PhoneDetailUpdate())
{
}
// If No different code
if(confirm("Would you like to delimit the record"))
{
//Delimit record code or return true;
}
else
{
return false;
}
Add following javascript function in the header of the page
<script type="text/javascript">
function update() {
var result = confirm("Do you want to delimit the record?")
if (result) {
}
else {
return false;
}
}
</script>
and then attach the event to button
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="return update();"/>
var ans = confirm("Do you want to delimit the record?")
if (ans){
//clicked on yes
}
else{
return false;
}

inline edit control

The label is initialized with the value of the textbox. Upon clicking the label, the textbox is shown. The user can then edit the contents of the textbox. Upon blurring focus, the textbox is hidden and the label shown. Should the user delete the contents of the textbox or only enter whitespace into the textbox, the textbox is not hidden, thus avoiding showing a label with no text. Is there a way to do this ?
Untested, but the general idea should help you out.
HTML:
<asp:TextBox ID="txtA" onblur="txtBlur();" style="display:none;" runat="server"/>
<asp:Label ID="txtA" onclick="txtFocus();" runat="server"/>
Client-side JS:
<script>
var txtA = document.getElementById("<%# txtA.ClientID %>");
var lblA = document.getElementById("<%# lblA.ClientID %>");
function txtBlur()
{
if (txtA.value.trim() != '')
{
lblA.innerText = txtA.value;
lblA.style.display = 'inline';
txtA.style.display = 'none';
}
}
function txtFocus()
{
txtA.value = lblA.innerText;
lblA.style.display = 'none';
txtA.style.display = 'inline';
}
</script>
Check for js validation that textbox is not empty
function Validate()
{
if(document.getElementById("txta").value=="")
{
alert('Please enter the value');
document.getElementById("txta").focus();
return false;
}
}
or you can server side
if (txa.text ="")
{
Response.Write('Text box cannot be empty');
}

Confirmation dialog at runtime in asp.net

I have a simple content management system that stores pages by Pagename and Version. After clicking on Save, my code (server side) checks for the existence of Pagename/Version.
If it exists I would like to display a confirmation dialog box, which asks the user to confirm whether or not the current Pagename/Version should be replaced.
What is the easiest way to accomplish this? Thanks.
<asp:Button OnClientClick="return confirm('Are you sure you want to go?');"
Text="Confirm" runat="server" onclick="Unnamed1_Click" />
If they click OK, the server onclick event will happen, if they click cancel, it will be like they didn't even press the button, of course, you can always add functionallity to the cancel part.
Maybe something like:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function CompareConfirm()
{
var str1 = "abc";
var str2 = "def";
if (str1 === str2) {
// your logic here
return false;
} else {
// your logic here
return confirm("Confirm?");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button OnClientClick="return CompareConfirm();"
Text="Confirm" runat="server" onclick="Unnamed1_Click" />
</div>
</form>
</body>
</html>
I appreciate both previous answers and they were helpful but not exactly what I was looking for. After considering the responses and doing more research I'm posting my solution so that maybe it will help someone else.
Button code:
<asp:Button ID="btnSave" OnClick="btnSaveClick" runat="server" Text="Save" OnClientClick="return CheckForVersion()" />
Javascript:
<script language="javascript">
function CheckForVersion() {
PageMethods.CheckForVersion(aspnetForm.ctl00$ContentPlaceHolder1$ddlPageName2.value, aspnetForm.ctl00$ContentPlaceHolder1$txtContentName2.value, OnSucceeded, OnFailed);
return false;
}
function OnSucceeded(results) {
if(results) {
//version exists so prompt user
if(confirm("Version already exists. Do you want to overwrite?")) {
__doPostBack('ctl00$ContentPlaceHolder1$btnSave','');
}
}
else
{
//version does not exist so save it without prompting user
__doPostBack('ctl00$ContentPlaceHolder1$btnSave','');
}
}
function OnFailed(error) {
// handle pagemethod error
alert(error.get_message());
}
</script>
C# using Subsonic 2.1:
[WebMethod]
public static bool CheckForVersion(string pageName, string versionName)
{
PageContentCollection pages = new PageContentCollection().Where("pageName", pageName).Where("versionName", versionName).Load();
if (pages.Count > 0)
return true;
else
return false;
}
An alternative, simpler approach which doesn't require AJAX would be to allow the post-back as normal, then in the code-behind, do your checks.
If the user confirmation is required, just return the user back to the same page but make an extra panel visible and hide the original 'Save' button.
In this extra panel, display your message with another OK / Cancel button. When the user clicks this OK button, perform the save!
Put the check before rendering the page to the client. Then attach a handler (on the client side, eg. javascript) to the save-button or form that displays the confirmation box (but only if the saving results in a replacement).
add a hidden field to your page for example Hiddenfield1
then add this function
public bool Confirm(string MSG)
{
string tmp = "";
tmp = "<script language='javascript'>";
tmp += "document.getElementById('HiddenField1').value=0; if(confirm('" + MSG + "')) document.getElementById('HiddenField1').value=1;";
tmp += "</script>";
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "ConfirmBox", tmp);
if(HiddenField1.Value.Trim()=="0") return false;
return true;
}

Categories