• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

几个常用的小函数(二)

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 //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)
        }
        if ((status==2) && (datestr.charAt(i)!=‘’-‘’))
        {
            tmpd=tmpd+datestr.charAt(i)
        }

    }
    year=new String (tmpy);
    month=new String (tmpm);
    day=new String (tmpd)
    //tempdate= new String (year+month+day);
    //alert(tempdate);
    if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
    {
        //alert("Invalid format of date!");
        return 0;
    }
    if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
    {
        //alert ("Invalid month or day!");
        return 0;
    }
    if (!((year % 4)==0) && (month==2) && (day==29))
    {
        //alert ("This is not a leap year!");
        return 0;
    }
    if ((month<=7) && ((month % 2)==0) && (day>=31))
    {
        //alert ("This month is a small month!");
        return 0;
    
    }
    if ((month>=8) && ((month % 2)==1) && (day>=31))
    {
        //alert ("This month is a small month!");
        return 0;
    }
    if ((month==2) && (day==30))
    {
        //alert("The Febryary never has this day!");
        return 0;
    }
    
    return 1;
}
//函数名:chksafe
//功能介绍:检查是否含有"‘’",‘’\\‘’,"/"
//参数说明:要检查的字符串
//返回值:0:是  1:不是
function chksafe(a)
{    
    return 1;
/*    fibdn = new Array ("‘’" ,"\\", "、", ",", ";", "/");
    i=fibdn.length;
    j=a.length;
    for (ii=0;ii<i;ii++)
    {    for (jj=0;jj<j;jj++)
        {    temp1=a.charAt(jj);
            temp2=fibdn[ii];
            if (tem‘’;p1==temp2)
            {    return 0; }
        }
    }
    return 1;
*/    
}

//函数名:chkspc
//功能介绍:检查是否含有空格
//参数说明:要检查的字符串
//返回值:0:是  1:不是
function chkspc(a)
{
    var i=a.length;
    var j = 0;
    var k = 0;
    while (k<i)
    {
        if (a.charAt(k) != " ")
            j = j+1;
        k = k+1;
    }
    if (j==0)
    {
        return 0;
    }
    
    if (i!=j)
    { return 2; }
    else
    {
        return 1;
    }
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网