如何在asp.net的Amount文本框(格式22.00)中编写javascript验证?

如何在asp.net的Amount文本框(格式22.00)中编写javascript验证?,javascript,asp.net,Javascript,Asp.net,我使用asp.net4.0在这个aspx页面一个文本框中输入速率,用户只输入数字(22)它可以显示22.00这样假设用户输入22.5它显示22.50请帮助我如何在客户端javascript中编写代码我这样写 function Rate(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 ||

我使用asp.net4.0在这个aspx页面一个文本框中输入速率,用户只输入数字(22)它可以显示22.00这样假设用户输入22.5它显示22.50请帮助我如何在客户端javascript中编写代码我这样写

function Rate(evt) {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
             return false;

         return true;
     }
功能速率(evt){
var charCode=(evt.which)?evt.which:event.keyCode
如果(字符码>31&(字符码<48 | |字符码>57))
返回false;
返回true;
}
它只需要数字,如何在dot中编写代码也可以尝试以下代码:

function isNumberWithDecimal(evt, source){
    evt =(evt)?evt :window.event;
    var charCode =(evt.which)?evt.which :evt.keyCode;
    var amt = source.value;
    var len = parseInt(amt.length);
    var ind = parseInt(amt.indexOf('.'));
    var cursorPosition = GetCursorLocation(source); 

    if(isNaN(amt)){
        evt.keyCode = 0;
        source.value = ".00";
    }
    if(charCode == 46){
        if(ind>-1)
                evt.keyCode = 0;
    }
    else if(charCode < 48 || charCode >57)
        evt.keyCode = 0;
    else{
        /* it will allow to enter only two digit after decimal point in numeric field      */
        if((len - ind)>2 && ind > -1 && cursorPosition > ind)
                evt.keyCode = 0;
    }
}
函数isNumberWithDecimal(evt,源代码){
evt=(evt)?evt:window.event;
var charCode=(evt.which)?evt.which:evt.keyCode;
var amt=来源价值;
var len=parseInt(金额长度);
var ind=parseInt(amt.indexOf('.');
var cursorPosition=GetCursorLocation(源);
if(伊斯南(金额)){
evt.keyCode=0;
source.value=“.00”;
}
if(charCode==46){
如果(ind>-1)
evt.keyCode=0;
}
否则如果(字符码<48 | |字符码>57)
evt.keyCode=0;
否则{
/*它只允许在数字字段中输入小数点后的两位数字*/
如果((len-ind)>2&&ind>-1&&cursorPosition>ind)
evt.keyCode=0;
}
}

我想你可以使用

试试这个:num.toPrecision(4)

例如:

var num = 22.5;
var stringrep = "";

if(!isNaN(num))stringrep =num.toPrecision(4);  //Action is here


alert(stringrep);