幾個常用的小函數(二)

來源:互聯網
上載者:User
函數 //opt1 小數     opt2   負數
//當opt2為1時檢查num是否是負數
//當opt1為1時檢查num是否是小數
//返回1是正確的,0是錯誤的
function chknbr(num,opt1,opt2)
{
    var i=num.length;
    var staus;
//staus用於記錄.的個數
    status=0;
    if ((opt2!=1) && (num.charAt(0)=='-'))
    {
        //alert("You have enter a invalid number.");
        return 0;
    
    }
//當最後一位為.時出錯
    if (num.charAt(i-1)=='.')
    {
        //alert("You have enter a invalid number.");
        return 0;
    }

    for (j=0;j<i;j++)
    {
        if (num.charAt(j)=='.')
        {
            status++;
        }
        if (status>1)
        {
        //alert("You have enter a invalid number.");
        return 0;        
        }
        if (num.charAt(j)<'0' || num.charAt(j)>'9' )
        {
            if (((opt1==0) || (num.charAt(j)!='.')) && (j!=0))
            {
                //alert("You have enter a invalid number.");
                return 0;
            }
        }
    }
    return 1;
}

//函數名:chkdate
//功能介紹:檢查是否為日期
//參數說明:要檢查的字串
//傳回值:0:不是日期  1:是日期
function chkdate(datestr)
{
    var lthdatestr
    if (datestr != "")
        lthdatestr= datestr.length ;
    else
        lthdatestr=0;
        
    var tmpy="";
    var tmpm="";
    var tmpd="";
    //var datestr;
    var status;
    status=0;
    if ( lthdatestr== 0)
        return 0

    
    for (i=0;i<lthdatestr;i++)
    {    if (datestr.charAt(i)== '-')
        {
            status++;
        }
        if (status>2)
        {
            //alert("Invalid format of date!");
            return 0;
        }
        if ((status==0) && (datestr.charAt(i)!='-'))
        {
            tmpy=tmpy+datestr.charAt(i)
        }
        if ((status==1) && (datestr.charAt(i)!='-'))
        {
            tmpm=tmpm+datestr.charAt(i)
     



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.