驗證輸入的正確性

來源:互聯網
上載者:User
/*

Date    : 2003-12-10

History : Create

Purpose : 驗證輸入資料的準確性,是否按照預定格式

*/

function ValidateDataType( strDataType ) 

{

    var Reg ;

    var sExample

    if ( strDataType.match( "money") )

    {

        Reg = /^\d+(,\d\d\d)*(\.\d*)?$/g ;//Deal as double

        sExample = "\n123  \n23456.78  \n23,456.78  \n0.12  \n.12  \n";

    }

    else if ( strDataType.match( "datetime") )

    {

        Reg = /^([1-2]\d{3})(-|\.)(0?[1-9]|10|11|12)(-|\.)(([0-2]?[1-9])|10|20|30|31)$/g ;

        sExample = "\n2003-08-24  2003.08.24\n1998-12-1  1998.12.1";

    }    

    else if ( strDataType.match( "numeric") )

    {

        Reg = /^\d+(,\d\d\d)*(\.\d*)?$/g ;

        sExample = "\n123  \n23456.78  \n23,456.78  \n0.12 \n";

    }

    else if ( strDataType.match( "wint") )

    {

        Reg = /^\-*\d+(,\d\d\d)*$/g ;

        sExample = "\n20  \n-1  \n";

    }        

    else if ( strDataType.match( "listint") )

    {

        Reg = /^\d+((,\d+)|(\-|\-\d+(,\d+)*))*$/g ;

        sExample = "\n8 \n1,2,4 \n1,2,6-8,12- \n";

    }

    else if ( strDataType.match( "int") )

    {

        Reg = /^\d+(,\d\d\d)*$/g ;

        sExample = "\n23456  \n23,456  \n";

    }    

    else if ( strDataType.match( "tinyint") )

    {

        //Reg = /^\d+(,\d\d\d)*$/g ;

        Reg = /^[0-2]+\d*$/g ;

        sExample = "\n255  \n255  \n";

    }

    else 

    {

        Reg = /^.*$/g ;

        sExample = "a2~!@#$%^&*()_+,./;':";

    }

    

    var e = window.event.srcElement;

    var str = e.value;

    if ( str == "" ) return;

    if ( Reg.exec( str )) return;

    alert(" 錯誤 : 值 [ " + str + " ] 非法," + " \n\n正確的格式如下所示 : " + sExample    ); 

    e.focus();

}


/*

Date    : 2003-12-10

History : Create

Purpose : 將浮點數格式化成只流小數兩位,按照四捨五入方式

*/

function roundFun(numberRound,roundDigit)

{

    var digit   = Math.pow(10,roundDigit);

    var numV    = Math.round(numberRound*digit)/digit;

    var strV    = numV.toString();

    var strZero = "";

   

    if(strV.indexOf(".") > -1)

    {

        for(var j=0; j<roundDigit-strV.split(".")[1].length; j++)

        {

            strZero += "0";

        }

    }

    else

    {

        //得到小數點後的字串

        strZero = ".";

        for(var j=0; j<roundDigit; j++)

        {

            strZero += "0";

        }

    }

    return strV+strZero;

}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.