We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Return a decimal from a asp.net textbox using client side javascript - Urgent

AnjanSaha
AnjanSaha asked
on
Medium Priority
275 Views
Last Modified: 2010-08-05
Hi..

I am having a as.net text box and a button.

What i need to do is as soon as the user enters an amount i need to validate whether it is
an integer or not and then if it is i need to return the integer in decimal places using a client
side javascript which i will pass to some other c# function.

For eg.

User inputs 2000 in text box. (ASPX page)

My javascript function will take this 2000, value return 2000.00 (in decimal places..i have written this function)

I will take this 2000.00 value and call some c# function.

Can anyone tell me how to do this using client side javascript with CODE .

Thanks in Advance.

-Anjan
Comment
Watch Question

using javascript

if (!isNAN(document.form1.textbox1.value))
   alert('not an integer');

remember javascript is case sensitive ..

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
can you tell me with entire code...

i also want the 2000 converted to 2000.00 and pass it to a function..


can u post the full code as i am new to this...
Hi AnjanSaha,
pls go to this link you can get all the functions needed to format numbers
http://www.eggheadcafe.com/articles/20031204.asp

Cheers!
Anil Nair
AnjanSaha,
http://www.eggheadcafe.com/articles/20031204.asp
if you are not able to get  above link
AnjanSaha,
tried copying the URL twice
but here goes the code

<script language=JavaScript>
 
function GetDecimalDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {

            case 3:  
                           
                           sRet = '#';
                           break;
           
            case 2:  
                           
                           sRet = ',';
                           break;
            default:
                           sRet = '.';
                           break;
 
        }

      return sRet;

}

function GetCommaDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {
           
            case 3:  
                           
                           sRet = '*';
                           break;
            case 2:  
                           
                           sRet = ',';
                           break;
            default:
                           sRet = ',';
                           break;
 
        }

      return sRet;

}

function FormatClean(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
     
   try
   {
       for(i=0;i<nVal;i++)
      {
         sChar = num.charAt(i);
         nChar = sChar.charCodeAt(0);
         if ((nChar >=48) && (nChar <=57))  { sVal += num.charAt(i);   }
      }
   }
    catch (exception) { AlertError("Format Clean",e); }
    return sVal;
}
 

function FormatCurrency(num,nCountryCode)
{      
        var sVal='';
        var minus='';
        var Decimal='';
        Decimal = GetDecimalDelimiter(nCountryCode);
        if (num.lastIndexOf("-") == 0) { minus='-'; }
        if (num.lastIndexOf(Decimal) < 0) { num = num + '00'; }
        num = FormatClean(num);
        sVal = minus + FormatDollar(num,GetCommaDelimiter(nCountryCode)) + GetDecimalDelimiter(nCountryCode) + FormatCents(num);
        return sVal;
}

function FormatNumber(num,nCountryCode)
{      
        var sVal='';
        var minus='';
        var CommaDelimiter='';

        try
       {

           CommaDelimiter = GetCommaDelimiter(nCountryCode);

           if (num.lastIndexOf("-") == 0) { minus='-'; }

           num = FormatClean(num);

           num = parseInt(num);

           var samount = new String(num);
             
           for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
          {
             samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
           }

        }
         catch (exception) { AlertError("Format Number",e); }
        return minus + samount;
}

function FormatCents(amount)
{
     var cents = '';

      try
      {
           amount = parseInt(amount);
           var samount = new String(amount);

           if (samount.length == 0) { return '00'; }
           if (samount.length == 1) { return '0' + samount; }
           if (samount.length == 2) { return samount; }
         
           cents =  samount.substring(samount.length -2,samount.length);
         
      }
      catch (exception) { AlertError("Format Cents",e); }
      return cents;
}

function FormatDollar(amount,CommaDelimiter)
{
   try
   {
 
        amount = parseInt(amount);

        var samount = new String(amount);

        if (samount.length < 3) { return 0; }  

        samount =  samount.substring(0,samount.length -2);
             
        for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
        {
           samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
         }

   }
    catch (exception) { AlertError("Format Comma",e); }
    return samount;
}

 function AlertError(MethodName,e)
 {
            if (e.description == null) { alert(MethodName + " Exception: " + e.message); }
            else {  alert(MethodName + " Exception: " + e.description); }
 }

</script>

on the blur event of the text box add the following lines "this.value=FormatCurrency(this.value,1);"
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.