Archive for the 'JavaScript' Category

Free Textbox With UpdatePanel

Hi,
As we often use Free Textbox control with our web projects. But it show some limitation when we use it with updatepanel. No this limitation removed using this work around of this problem

Step1
//Add this on page load
if(!Page.IsPostBack)
{
Page.ClientScript.RegisterClien

tScriptInclude(“FTB-FreeTextBox“, VirtualPathUtility.MakeRelative(Request.Path, “~/scripts/FTB-FreeTextBox.js”));
Page.ClientScript.RegisterClien
tScriptInclude(“FTB-Utility”, VirtualPathUtility.MakeRelative(Request.Path, “~/scripts/FTB-Utility.js”));
Page.ClientScript.RegisterClien
tScriptInclude(“FTB-Toolbars”, VirtualPathUtility.MakeRelative(Request.Path, “~/scripts/FTB-ToolbarItems.js“));
Page.ClientScript.RegisterClien
tScriptInclude(“FTB-ImageGallery”, VirtualPathUtility.MakeRelative(Request.Path, “~/scripts/FTB-ImageGallery.js“));
Page.ClientScript.RegisterClien
tScriptInclude(“FTB-Pro”, VirtualPathUtility.MakeRelative(Request.Path, “~/scripts/FTB-Pro.js”));
}

and add these two funcions
Step2
[Obsolete]
public override void RegisterStartupScript(string key, string script)
{
string newScript = script.Replace(“FTB_AddEvent
(window,’load’,function () {“, “”).Replace(“});”, “”);
ScriptManager.RegisterStartupSc
ript(this, typeof(Page), key, newScript, false);
}
public new void RegisterOnSubmitStatement
(string key, string script)
{
ScriptManager.RegisterOnSubmitS
tatement(this, typeof(Page), key, script);
}

Cheers, Now textbox work very well in update panel.

How to Redirect in Asp.Net Ajax?

Hi,

In asp.net ajax we can’t redirect to another page using server.transfer or Response.redirect.

So this is big problem, for exaple we want to redirect to login page if user is not logged in or when user click a button to perform action we want to check if some condition true then redirect topage1 else redirect to page2.

for this I use following code.

if(condition1)

{

ScriptManager.RegisterStartupScript(yourUpdatePanelName, this.GetType(), “redirectMe”, “location.href=’page1.aspx’;”, true);

}

else

{

ScriptManager.RegisterStartupScript(yourUpdatePanelName, this.GetType(), “redirectMe”, “location.href=’page2.aspx’;”, true);

}

Thanks

How to trim a string in javascript?

Use these functions that i write below.

function leftTrim(sString)
{
while (sString.substring(0,1) == ‘ ‘)
{
sString = sString.substring(1, sString.length);
}
return sString;
}

function rightTrim(sString)
{
while (sString.substring(sString.length-1, sString.length) == ‘ ‘)
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

function trimAll(sString)
{
while (sString.substring(0,1) == ‘ ‘)
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ‘ ‘)
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

Just pass string to trim function, It automatically remove the white space from start and end.

How to verify credit card number using javascript?

If you want to verify credit card number on client system before he submit the form. You need this javascript function that I wrote below.

function vaildateCreditCard(cardNumber, cardType)
{
var isValid = false;
var ccCheckRegExp = /[^\d ]/;
isValid = !ccCheckRegExp.test(cardNumber);

if (isValid)
{
var cardNumbersOnly = cardNumber.replace(/ /g,””);
var cardNumberLength = cardNumbersOnly.length;
var lengthIsValid = false;
var prefixIsValid = false;
var prefixRegExp;

switch(cardType)
{
case “Mastercard”:
lengthIsValid = (cardNumberLength == 16);
prefixRegExp = /^5[1-5]/;
break;

case “Visa”:
lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
prefixRegExp = /^4/;
break;

case “American Express”:
lengthIsValid = (cardNumberLength == 15);
prefixRegExp = /^3(4|7)/;
break;

case “Discover”:
lengthIsValid = (cardNumberLength == 16);
prefixRegExp = /^6011/;
break;

default:
return false;
}

prefixIsValid = prefixRegExp.test(cardNumbersOnly);
isValid = prefixIsValid && lengthIsValid;
}

if (isValid)
{
var numberProduct;
var numberProductDigitIndex;
var checkSumTotal = 0;

for (digitCounter = cardNumberLength – 1;
digitCounter >= 0;
digitCounter–)
{
checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
digitCounter–;
numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
for (var productDigitCounter = 0;
productDigitCounter < numberProduct.length;
productDigitCounter++)
{
checkSumTotal +=
parseInt(numberProduct.charAt(productDigitCounter));
}
}

isValid = (checkSumTotal % 10 == 0);
}

return isValid;
}

Just pass credit card number and its type, it automatically verify it.

How to round decimal place number to 2 digit?

If you want to round your number to 2 digit at decimal place then you can use this method that automatically round the number after getting the input from user.

Write this in javascript

<script language=”javascript” type=”text/javascript”>
function chkValue(txt)
{
var value=txt.value;
var result=Math.round(value*100)/100;
txt.value = result;
}
</script>

And call it onBlur as below I call it.

asp:TextBox ID=”txtSublet” runat=”server” Width=”50px” onBlur=”return chkValue(this)”></asp:TextBox>