dispaly error message with out alert box using javascript - c#

i want to display error message without alert box by using javascript.
i have tried with code.
function validatetextbox() {
var txtuname = document.getElementById('<%=txt_uname.ClientID %>').value;
if(txtuname=="") {
document.getElementById("text_uname").innerHTML= "Enter Username"; } }
but it is not working, please give some suggestions

You need to use document.getElementById('<%=txt_uname.ClientID %>') instead of document.getElementById("text_uname")
Complete function
function validatetextbox() {
var txtuname = document.getElementById('<%=txt_uname.ClientID %>').value;
if (txtuname == "") {
document.getElementById('<%=txt_uname.ClientID %>').innerHTML = "Enter Username";
}
}

try to cache dom, and set innerHTML
function validatetextbox() {
var inputEl = document.getElementById('<%=txt_uname.ClientID %>'),
txtuname = inputEl.value;
if ( txtuname == "" ) {
inputEl.innerHTML = "Enter Username";
}
}

It seems like you need to show alert in txt_uname .. then you should do like this:
function test(){
var txtuname =document.getElementById('<%=txt_uname.ClientID %>').value;
if(txtuname == "") {
document.getElementById('<%=txt_uname.ClientID %>').value= "Enter Username";
}
}

try this
function test()
{
var row = lnk.parentNode.parentNode;
var txtuname= row.cells[your cell number].childNodes[your node number].innerHTML;
if(txtuname=="")
{
txtuname="enter username";
}
}

Related

How Can I use switch statement instead of too many else if

Hello please help me out in writing switch statement for the below else if statement
if(ClientAddressTextBox.Text == "")
{
MessageBox.Show("Please Enter Client Address");
this.ActiveControl = ClientAddressTextBox;
}
else if (InnerpathTextBox.Text == "")
{
MessageBox.Show("Please Enter Internal Path");
this.ActiveControl = InnerpathTextBox;
}
else if (InspectorIDTextBox.Text == "")
{
MessageBox.Show("Please Enter Inspector ID");
this.ActiveControl = InspectorIDTextBox;
}
else if (SerialNumberTextBox.Text == "")
{
MessageBox.Show("Please Enter Serial Number");
this.ActiveControl = SerialNumberTextBox;
}
You could do something like this:
var controls = new []
{
new {Ctrl = InnerpathTextBox, Error = "Please Enter Client Address"},
new {Ctrl = ClientAddressTextBox, Error = "Please Enter Internal Path"},
new {Ctrl = InspectorIDTextBox, Error = "Please Enter Inspector ID"},
new {Ctrl = SerialNumberTextBox, Error = "Please Enter Serial Number"}
};
var firstToFailValidation = controls.FirstOrDefault(item => item.Ctrl.Text == "");
if (firstToFailValidation != null)
{
MessageBox.Show(firstToFailValidation.Error);
this.ActiveControl = firstToFailValidation.Ctrl;
}
You might want to check for nulls though. This code assumes that none of the controls or the .Text properties are null.
I'd approach this as follows:
var validationMessages = new[]{new{Control = InnerpathTextBox,
Message = "Please Enter Internal Path"},
new{Control = InspectorIDTextBox,
Message = "Please Enter Inspector Id"},
//etc
};
foreach(var vm in validationMessages)
{
if(string.IsNullOrWhiteSpace(vm.Control.Text))
{
MessageBox.Show(vm.Message);
this.ActiveControl = vm.Control;
break;
}
}
You can use Something like this:
private bool IfTextBoxEmpty(string text, string message, Control control)
{
if (text == "")
{
MessageBox.Show(message);
this.ActiveControl = control;
return true;
}
return false;
}
And the Usage:
if (!IfTextBoxEmpty(ClientAddressTextBox.Text, "Please Enter Client Address", ClientAddressTextBox)
{
//if not - Do Something
}
This is a pseudo code for a switch statement.
string statement;
switch(statement)
{
case (ClientAddressTextBox.Text == ""):
MessageBox.Show("Please Enter Client Address");
this.ActiveControl = ClientAddressTextBox;
break;
case (InnerpathTextBox.Text == ""):
...
}
and so on.

Read asp.net dynamic textbox content length using c#

I am using the below code to create a dynamic textbox and their onchange event.Event fired successfully and it doesn't return any value.Please help me to solve this.
txt_box.Attributes.Add("onchange", "loadValues('" + txt_box.ClientID + "')");
function loadValues(controlName) {
alert(controlName);
//control name comes here
var txtValue = document.getElementById(controlName);
//control also return null
if (txtValue.value.length > 0)
{
alert(txtValue.value.length);
}
}
Was just about to answer the same as Ankush Jain, but then none jquery version:
function loadValues(control) {
alert(control.id);
//control name comes here
var txtValue = control.value;
//control also return null
if (txtValue.length > 0) {
alert(txtValue.length);
}
}
txt_box.Attributes.Add("onchange", "loadValues(this);");
try the following
txt_box.Attributes.Add("onchange", "loadValues(this)");
function loadValues(controlName) {
if($(controlName).attr('id').length > 0){
var id= $(controlName).attr('id');
var val= $('#'+id).val();
alert(val);
}
}

Textbox Changes are not Reflecting in JavaScript

I have the following JavaScript function. I have two textboxes, two hiddenfields and two labels. and a button. On the click of a button I am calling this function.
On typing value in the textbox, It shows an autoCompleteList. On picking a value from this, it fetches the value from DB and fills the corresponding label. Also, at the same time it stores the corresponding selected value in hiddenfield.
Whenever one changes the value in textbox and without selecting the value from autoPopulated list, It must show the alert.
The function is working fine, If I fill a single textbox, but when I fill both, It goes to the Point A, instead of showing the alert of Point B
function ConfirmBox()
{
var txtBoxValue = document.getElementById("<%= txtFromPartNumber.ClientID %>").value;
var txtBoxValue1 = document.getElementById("<%= txtToPartNumber.ClientID %>").value;
var release = document.getElementById("<%= lblFromPartNumber.ClientID %>").innerText;
var release1 = document.getElementById("<%= lblToPartNumber.ClientID %>").innerText;
var hdnFromValueID = "<%= hdnFromReleaseNumber.ClientID %>";
var hdnToValueID = "<%= hdnToReleaseNumber.ClientID %>";
if ((txtBoxValue && txtBoxValue.trim() != "" && release))
{
if (document.getElementById(hdnFromValueID).value.trim() == txtBoxValue.trim())
{
if ((txtBoxValue1 && txtBoxValue1.trim() != "" && release1))
{
if (document.getElementById(hdnToValueID).value.trim() == txtBoxValue1.trim())
{
return true; //Point A
}
else
{// Point B
//alert("To Part no. has been changed. Please enter it again.");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "To Part no. has been changed. Please enter it again.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
document.getElementById("<%= txtToPartNumber.ClientID %>").focus();
return false;
}
}
else
{
//window.alert("Please choose a Valid To Part number");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "Please choose a Valid To Part number.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
return false;
}
}
else
{
//alert("From Part no. has been changed.Please enter it again.");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "From Part no. has been changed.Please enter it again.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
document.getElementById("<%= txtFromPartNumber.ClientID %>").focus();
return false;
}
}
else
{
//window.alert("Please choose a Valid From Part number");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "Please choose a Valid From Part Number.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
return false;
}
}

Display predefined ***** in asp.net TextBox when TextMode = "Password"

The TextBox with TextMode = "Password" will be empty after assign a value to it's text property.
How can I set predefined password for my password textbox?
Also I want to use this jQuery code for my textbox :
function onclickOfPassword(This) {
if (This.value == 'Password') {
This.value = '';
}
}
function onblurOfPassword(This) {
if (This.value == '') {
This.value = 'Password';
}
}
you can do like this....using jquery .....
by using below function we can show and hide two different inputs. You need set one with ID Password and another with ID PasswordDummy, For clients with no javascript, best to set PasswordDummy to display:none initially.
$(‘input’).each(function()
{
if (this.id == ‘Password’) {
// Handle Password differently – it only gets an onblur, in which it gies invisible and activates the PasswordDummy if it is empty
// if its blank, make it invisible and Dummy visible
if (this.value == ”)
{
$(this).hide();
$(“#PasswordDummy”).show();
}
else
{
$(this).show();
$(“#PasswordDummy”).hide();
}
$(this).blur(function()
{
if (this.value == ”) {
$(this).hide();
$(“#PasswordDummy”).show();
}
else
{
$(this).show();
$(“#PasswordDummy”).hide();
}
});
}
else if (this.id == ‘PasswordDummy’) {
// Handle Password Dummy differently
this.value = $(this).attr(‘title’);
$(this).addClass(‘text-label’);
$(this).focus(function()
{
$(this).hide();
$(“#Password”).show();
$(“#Password”).focus(); });
}
else if ($(this).attr(‘title’) != undefined)
{
if (this.value == ”)
{
this.value = $(this).attr(‘title’);
$(this).addClass(‘text-label’);
}
$(this).focus(function()
{
if (this.value == $(this).attr(‘title’)) {
this.value = ”;
$(this).removeClass(‘text-label’);
}});
$(this).blur(function()
{
if (this.value == ”) {
this.value = $(this).attr(‘title’);
$(this).addClass(‘text-label’);
}});
}
});

Why can't JQuery load my resource?

I have the following script in an page called ajax.aspx page:
<script type="text/javascript">
$(document).ready(function () {
var nameFoundMessage = $('#nameFoundMessage');
var nameInput = $('#name');
nameFoundMessage.hide();
nameInput.blur(function () {
if ($(this).val()) {
$.getJSON('Services/ArtistFound.aspx?' + escape($(this).val()), function (results) {
if (results.available) {
if (nameFoundMessage.is(':visible')) {
nameFoundMessage.html('The name was found');
}
}
else {
nameFoundMessage.show();
nameFoundMessage.html('The name was not found');
}
});
}
});
});
</script>
The page has an input field with an id of "name" and when I blur off of that it goes into a service folder which has another aspx page ArtistFound.aspx and in that Page load, I have the following:
Response.ContentType = "application/json";
string name = Request.QueryString.ToString();
string output = string.Empty;
name = db.Names.Single(x => x.Name== name).Name;
if(name == null)
{
output = "{available:false}";
}
else
{
output = "{available:true}";
}
Response.Write(output);
}
When I run the page and blur off the input, it says the following:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I have tried ../Services/ArtistFound.aspx... and /Services/ArtistFound.aspx..., but it still gives me the same error.
You want:
name = db.Names.FirstOrDefault(x => x.Name== name);
if(name != null && name.Name != null)
{
output = "{available:true}";
}
else
{
output = "{available:false}";
}
This will return null if it is not found rather than throwing an exception like Single() does.
I would also recommend you use an ASHX handler rather than an ASPX page to do this call.
To do this you just add a 'Generic Handler' file in visual studio then you can put replace the ProcessRequest method with this:
public void ProcessRequest(HttpContext context)
{
string name = context.Request.QueryString.ToString();
string output = string.Empty;
name = db.Names.FirstOrDefault(x => x.Name == name);
if (name != null && name.Name != null)
{
output = "{available:true}";
}
else
{
output = "{available:false}";
}
context.Response.ContentType = "application/json";
context.Response.Write(output);
}

Categories

Resources