`
foolraty
  • 浏览: 400217 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
文章分类
社区版块
存档分类
最新评论

输入日期格式,只能在规定的地方输入(转)

阅读更多
<html> 
<head> 
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "> 
<title> new   page </title> 

<SCRIPT LANGUAGE="JavaScript">
/*------------------------------------------------*/
//more javascript from http://Www.VeryAsp.Net
/*
* Added by LiuXiaoChong 2005.4.25
* 限制输入的日期控件
* Param: txtName 为要限制输入的文本框的名称
*
* 功能描述:1,只能输入数字
*                    2,左右键可以移动编辑焦点
*                    3,上下键可以对数据进行微调
*                    4,控件包含了对日期的合法校验
*/
function regDateControl(txtName)
{
    var oInput = document.getElementById(txtName);
    oInput.middleChar = "-";
    oInput.selectIndex = 1; //默认选中年
    oInput.maxLength = 10;
    oInput.style.imeMode = "disabled";
    oInput.value = specialText_GetDate(oInput.middleChar);
    oInput. charWidth = oInput.createTextRange().boundingWidth/oInput.maxLength;

    //注册单击事件
    oInput.onclick = specialText_ClickEvent;
    oInput.onkeydown = specialText_KeyDownEvent;
    oInput.onfocus = function(){specialText_SelectYear(this);}
    oInput.onblur = function()
    {
        specialText_validYear(this);
        specialText_validMonth(this);
        specialText_validDate(this);
    }
    //屏蔽鼠标右键和拖动操作
    oInput.oncontextmenu = function(){return false;}
    oInput.ondrop = function(){return false;}
}

//鼠标单击,根据位置对日期进行分体选择
function specialText_ClickEvent()
{
    event.cancelBubble = true;
    specialText_validYear(this);
    specialText_validMonth(this);
    specialText_validDate(this);
    if(event.offsetX <= specialText_getCharWidth(this.charWidth,4))
        specialText_SelectYear(this);
    else if(event.offsetX > specialText_getCharWidth(this.charWidth,4)
            && event.offsetX <= specialText_getCharWidth(this.charWidth,7))
        specialText_SelectMonth(this);
    else if(event.offsetX > specialText_getCharWidth(this.charWidth,7))
        specialText_SelectDate(this);
}
//选中年份
function specialText_SelectYear(oInput)
{
    var oRange = oInput.createTextRange();
    oRange.moveStart("character",0);
    oRange.moveEnd("character",-6);
    //代表选中了年
    oInput.selectIndex = 1;
    oRange.select();
}
//选中月份
function specialText_SelectMonth(oInput)
{
    var oRange = oInput.createTextRange();
    oRange.moveStart("character",5);
    oRange.moveEnd("character",-3);
    //代表选中了月
    oInput.selectIndex = 2;
    oRange.select();
}
//选中日期
function specialText_SelectDate(oInput)
{
    var oRange = oInput.createTextRange();
    oRange.moveStart("character",8);
    //代表选中了日期
    oInput.selectIndex = 3;
    oRange.select();
}
//校验年份有效性
function specialText_validYear(oInput)
{
    var arrValue = oInput.value.split(oInput.middleChar);
    var strYear = arrValue[0];

    if(parseInt(strYear,10) == 0)
        arrValue[0] = 2000;
    //如果年份小于4位,则在2000基础上增加
    else if(strYear.length < 4)
        arrValue[0] = 2000 + parseInt(strYear,10);
    oInput.value = arrValue.join(oInput.middleChar);
}
//校验月份有效性
function specialText_validMonth(oInput)
{
    var arrValue = oInput.value.split(oInput.middleChar);
    var strMonth = arrValue[1];
    //如果月份输入了0,则按1月处理
    if(parseInt(strMonth,10) == 0)
         arrValue[1] = "01";
    //如果月份是一位,则前面补0
    else if(strMonth.length < 2)
        arrValue[1] = "0" + strMonth;
    //如果月份大于12月,自动转为12月
    else if(parseInt(strMonth,10) > 12)
        arrValue[1] = "12";
    oInput.value = arrValue.join(oInput.middleChar);
}
//校验日期有效性
function specialText_validDate(oInput)
{
    var arrValue = oInput.value.split(oInput.middleChar);
    var strYear = arrValue[0];
    var strMonth = arrValue[1];
    var strDate = arrValue[2];
    var intMonth = parseInt(strMonth,10);
    if(parseInt(strDate,10) == 0)
        arrValue[2] = "01";
    else if(strDate.length < 2)
        arrValue[2] = "0" + strDate;
    else
    {
        //如果超过了月份的最大天数,则置为最大天数
        var monthMaxDates = specialText_getMonthDates(strYear,strMonth);
        if(parseInt(strDate,10) > monthMaxDates)
            arrValue[2] = monthMaxDates;
    }
    oInput.value = arrValue.join(oInput.middleChar);
}

function specialText_YearAdd(oInput,isMinus)
{
    var arrValue = oInput.value.split(oInput.middleChar);
    var strYear = arrValue[0];
    if(isMinus)
    {
        arrValue[0] = parseInt(strYear,10) - 1;
        if(parseInt(arrValue[0],10) < 1)
            arrValue[0] = "0001";
    }
    else
        arrValue[0] = parseInt(strYear,10) + 1;
    oInput.value = arrValue.join(oInput.middleChar);
    specialText_validYear(oInput);
    specialText_SelectYear(oInput);
}

function specialText_MonthAdd(oInput,isMinus)
{
    var arrValue = oInput.value.split(oInput.middleChar);
    var strMonth = arrValue[1];
    if(isMinus)
    {
        arrValue[1] = parseInt(strMonth,10) - 1;
        if(parseInt(arrValue[1],10) == 0)
            arrValue[1] = "12";
    }
    else
    {
        arrValue[1] = parseInt(strMonth,10) + 1;
        if(parseInt(arrValue[1],10) == 13)
            arrValue[1] = "01";
    }
    oInput.value = arrValue.join(oInput.middleChar);
    specialText_validMonth(oInput);
    specialText_SelectMonth(oInput);
}

function specialText_DateAdd(oInput,isMinus)
{
    var arrValue = oInput.value.split(oInput.middleChar);
    var strYear = arrValue[0];
    var strMonth = arrValue[1];
    var strDate = arrValue[2];
    var monthMaxDates = specialText_getMonthDates(strYear,strMonth);

    if(isMinus)
    {
        arrValue[2] = parseInt(strDate,10) - 1;
        if(parseInt(arrValue[2],10) == 0)
            arrValue[2] = monthMaxDates;
    }
    else
    {
        arrValue[2] = parseInt(strDate,10) + 1;
        if(parseInt(arrValue[2],10) == (monthMaxDates + 1))
            arrValue[2] = "01";
    }
    oInput.value = arrValue.join(oInput.middleChar);
    specialText_validDate(oInput);
    specialText_SelectDate(oInput);
}

function specialText_KeyDownEvent()
{
    //如果按了数字键
    if((event.keyCode >= 48 && event.keyCode <= 57) ||
       (event.keyCode >= 96 && event.keyCode <= 105))
    {     
        var oRange = document.selection.createRange();
        if(oRange.text.indexOf(this.middleChar) != -1)
            event.returnValue = false;
        else
            event.returnValue = true;
    }
    //如果按了方向键
    else if(event.keyCode >= 37 && event.keyCode <= 40)
    {
        event.returnValue = false;
        var keyCode = event.keyCode;
        //按了左键
        if(keyCode == 37)
        {
            if(this.selectIndex == 1)
            {
                specialText_validYear(this);
                specialText_SelectDate(this);
            }
            else if(this.selectIndex == 2)
            {
                specialText_validMonth(this);
                specialText_SelectYear(this);
            }
            else if(this.selectIndex == 3)
            {
                specialText_validDate(this);
                specialText_SelectMonth(this);
            }
        }
        //按了右键
        if(keyCode == 39)
        {
            if(this.selectIndex == 1)
            {
                specialText_validYear(this);
                specialText_SelectMonth(this);
            }
            else if(this.selectIndex == 2)
            {
                specialText_validMonth(this);
                specialText_SelectDate(this);
            }
            else if(this.selectIndex == 3)
            {
                specialText_validDate(this);
                specialText_SelectYear(this);
            }
        }

        //按了上键
        if(keyCode == 38)
        {
            if(this.selectIndex == 1)
            {
                specialText_validYear(this);
                specialText_YearAdd(this,true);
            }
            else if(this.selectIndex == 2)
            {
                specialText_validMonth(this);
                specialText_MonthAdd(this,true);
            }
            else if(this.selectIndex == 3)
            {
                specialText_validDate(this);
                specialText_DateAdd(this,true);
            }
        }

        //按了下键
        if(keyCode == 40)
        {
            if(this.selectIndex == 1)
            {
                specialText_validYear(this);
                specialText_YearAdd(this,false);
            }
            else if(this.selectIndex == 2)
            {
                specialText_validMonth(this);
                specialText_MonthAdd(this,false);
            }
            else if(this.selectIndex == 3)
            {
                specialText_validDate(this);
                specialText_DateAdd(this,false);
            }
        }
    }
    //如果按了F5 或 TAB,不屏蔽
    else if(event.keyCode == 116 || event.keyCode == 9)
        event.returnValue = true;
    else
    {
        event.returnValue = false;
        event.keyCode = 0;
    }
}

/*---------------------辅助函数-----------------------*/
//得到默认日期
function specialText_GetDate(middleChar)
{
    var oDate = new Date();
    return oDate.getYear() + middleChar
           + (oDate.getMonth() < 10 ? ("0" + oDate.getMonth()) : oDate.getMonth()) + middleChar
           + (oDate.getDate() < 10 ? ("0" + oDate.getDate()) : oDate.getDate());
}
//得到字符像素宽度
function specialText_getCharWidth(charWidth,charNum)
{
    return charNum * charWidth;
}
//得到某年某月的最大天数
function specialText_getMonthDates(strYear,strMonth)
{
    var intMonth = parseInt(strMonth,10);
    if(intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7
       || intMonth == 8 || intMonth == 10 || intMonth == 12)
        return 31;
    //处理30天的月份
    else if(intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11)
        return 30;
    //处理2月份
    else
    {
        //闰年
        if(specialText_isLeapYear(strYear))
            return 29;
        //平年
        else
            return 28;
    }
}
//判断是否是闰年
function specialText_isLeapYear(strYear)
{
    var intYear = parseInt(strYear,10);
    if((intYear % 4 == 0 && intYear % 100 != 0) ||
       (intYear % 100 == 0 && intYear % 400 == 0))
        return true;
    else
        return false;
}

function init()
{
    regDateControl('date1');
}
</SCRIPT>


</head> 
<body onload="init()">  
<INPUT TYPE="text" NAME="date1">
</body> 
</html>




http://www.veryasp.net/JavaScript/JavaScript.asp
分享到:
评论

相关推荐

    完整版日期格式规范.rar

    3. **欧洲日期格式(DD/MM/YYYY)**:在欧洲和其他地方,人们倾向于先写日子再写月份,如07/04/2023。同样,为了清晰起见,建议使用斜杠或点作为分隔符。 4. **自定义日期格式**:根据项目需求,开发者有时会创建...

    SAS (统计分析软件)课件:第13章 变量输入输出格式.ppt

    日期时间输入格式形式为`$INFORMAT &lt;W&gt;.&lt;D&gt;`,其中W规定输入数据的列数,D规定数值输入格式中保留小数的位数。 五、缺失值处理 SAS系统提供了多种缺失值处理方式,例如使用点(.)表示缺失值、使用BZ.输入格式把尾部...

    jQuery控制文本框内输入时间格式

    在本篇文章中,我们将深入探讨如何使用jQuery来控制文本框内的输入时间格式,确保用户只能按照规定的格式(例如“09:00:00”或“17:00:00”)输入时间数据。这不仅是前端验证的一个常见需求,也是提高用户体验的有效...

    MaskedTextBox来区分正确和不正确的用户输入.rar

    在Windows应用程序开发中,我们经常需要对用户的输入进行格式化,确保他们按照特定的规则进行输入,例如电话号码、日期、邮箱地址等。在这种情况下,`MaskedTextBox`控件是.NET Framework提供的一种非常有用的工具。...

    Java编程输入两个年月日,(1)判断两个日期是否相同,并且将相差的天数显示出来;(2)将日期后推或前推N天,并将该日期输出。

    在Java中处理日期和时间通常有几种方法,这里主要介绍`java.util.Date`类和`java.time`包下的相关类。不过,在这段代码中,开发者选择了一种自定义的方式来实现日期的操作。 #### 1.1 日期比较与计算天数差 **目标...

    专题资料(2021-2022年)SAS系统和数据分析输入输出格式.doc

    输入格式指定SAS如何读取数据,输出格式则规定了数据的展示方式。例如,`$informat&lt;w&gt;.&lt;d&gt;`用于字符输入,`&lt;informat&gt;w.&lt;d&gt;`用于数值输入,`$format&lt;w&gt;.&lt;d&gt;`和`&lt;format&gt;w.&lt;d&gt;`则分别对应字符和数值输出。其中,`w`...

    Android-BankCardUtils自动格式化银行卡号手机号身份证号输入的工具类

    其次,自动格式化输入的银行卡号是一种常见的优化措施,目的是使用户在输入长串数字时能更清晰地看到已输入的内容。BankCardUtils可能采用了每4位数字添加一个空格的策略,如“1234 5678 9012 3456”,使得用户在...

    matlab开发-将正常日期转换为朱利安达特

    2. 计算相对于特定基准日期的天数:朱利安日期是从公元前4713年1月1日中午12点开始的,因此需要计算输入日期与这个基准日期之间的天数差。 3. 转换为朱利安日期:基准日期的朱利安日期是0,所以输入日期的朱利安日期...

    JavaScript_在键入时格式化输入文本内容.zip

    在这个例子中,`#formattedInput`是你要格式化的输入元素,`numericOnly: true`限制只能输入数字,`delimiter: '-'`设置了分隔符,`blocks: [4, 4, 4, 4]`规定了每组数字的长度,这样用户在输入信用卡号或其他类似...

    日期输入框测试用例设计

    - 输入2009/10/10,如果项目的日期格式规定为2009-10-10,则可能会提示错误。 4. **安全性检查示例:** - 输入“ Hello everybody”,应该提示错误。 - 尝试通过拷贝粘贴的方式输入上述脚本代码,也应该提示...

    如何在ASP中选择日期

     1、在文本框中让用户按规定好的日期格式直接输入。这种方法最简单,可是用户使用起来很麻烦,而且程序员还要在后台对输入的日期进行数据验证,所以显得很笨拙;  2、用下拉列表列出年份、月份、天数由用户选择...

    VB适用的多输入格式的TextBox输入控件.rar

    VB适用的多输入格式的TextBox输入控件,比如这个控件可进行一些输入的限制,比如只允许输入大写字母、必须输入整数,必须输入小数,必须输入日期时间格式,只允许输入数字和小数点,允许任意字符等,若输入规定之外...

    C编写的万年历程序,输入年份,输入要显示的格式,就会得到结果,用于练习循环语句的控制。

    如果是源代码,我们可以期待看到C语言的函数定义和调用,用于处理日期计算、用户输入处理和输出格式化。如果是可执行文件,用户可以直接运行查看万年历效果。 在编写这样的程序时,开发者可能会使用以下C语言的关键...

    struts2(时间日期类型转换器)

    当用户在表单中输入日期数据时,Struts2会尝试根据配置的日期格式将这些数据转换为日期对象。如果用户输入的格式与预设格式不匹配,转换可能会失败,这时需要自定义转换器。 自定义转换器可以通过实现`...

    第一章 日期转换小工具.pptx

    此外,对于作业提交,需按照指定格式命名文件,并在规定的时间内通过课代表提交,以确保成绩的有效记录。 通过本课程的学习,学生不仅能够掌握C语言的基础知识,还能提升对日期处理的实际操作能力,为后续的软件...

    根据年月日计算周日期的小程序

    在这个小程序中,可能会使用到`java.time.LocalDate`来表示年月日,然后结合`java.time.temporal.ChronoUnit`来计算两个日期之间的周数差,从而得到输入日期所在的一周。 控制台是用户与程序交互的简单界面,通常在...

    JavaScript客户端输入验证

    JavaScript客户端输入验证是Web开发中不可或缺的一环,它主要用于在用户提交数据到服务器之前检查数据的有效性和格式。这种验证可以防止无效数据的提交,减轻服务器的负担,提高用户体验,因为错误提示可以即时显示...

    下一天日期计算

    下一天日期计算 ++下一天.cpp

    C编写NextDate函数

    该函数实现以下功能: ...输入日期年月日超出范围(如2004,11,32),输出‘无效输入日期’; 3.输入日期年月日均合法,但组合错误(主要指日错误如2011,2,29), 输出'日期组合错误'. 规定年为:1812年&lt;=2012

Global site tag (gtag.js) - Google Analytics