Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-02
8
Medium Priority
?
258 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
0
Comment
Question by:AnjanSaha
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
Rejojohny earned 1000 total points
ID: 16354855
using javascript

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

remember javascript is case sensitive ..
0
 

Author Comment

by:AnjanSaha
ID: 16354868
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...
0
 
LVL 1

Expert Comment

by:anilnairpkd
ID: 16357152
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
0
 
LVL 1

Expert Comment

by:anilnairpkd
ID: 16357163
AnjanSaha,
http://www.eggheadcafe.com/articles/20031204.asp
if you are not able to get  above link
0
 
LVL 1

Assisted Solution

by:anilnairpkd
anilnairpkd earned 1000 total points
ID: 16357183
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);"
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question