允许用户仅更改输入JavaScript中的某些值

允许用户仅更改输入JavaScript中的某些值,javascript,input,textbox,Javascript,Input,Textbox,我有以下代码 <input id="startDate "type="text" name="courseDate" value="MM/YYYY" /> 我想知道如何使用JavaScript确保用户只能编辑文本框中的“MM”和“yyy” 谢谢 Y查看下面的JSFIDLE。这听起来与您正在尝试的操作类似: 这是在使用jQuery库 以下是实施的一个示例: <script type="text/javascript">//<![CDATA[ $(window

我有以下代码

<input id="startDate "type="text" name="courseDate" value="MM/YYYY" />

我想知道如何使用JavaScript确保用户只能编辑文本框中的“MM”和“yyy”

谢谢
Y

查看下面的JSFIDLE。这听起来与您正在尝试的操作类似:

这是在使用jQuery库

以下是实施的一个示例:

<script type="text/javascript">//<![CDATA[ 
$(window).load(function(){
$("#date").mask("99/99/9999");
});//]]>  
</script>
//
使用html5日期输入:

<input type="date" max="2012-06-25" min="2011-08-13" name="the_date">


这不是个好主意。用户可以随时禁用JavaScript,您必须处理浏览器兼容性以获得光标位置等。最好在两个输入之间加上破折号(并且可能使用包装器将它们设置为一个输入):


/

另外,通常最好选择月份(也可能是年份),而不是文本输入。但这不是必须的。

这里有一些伪代码,概述了您想要使用的过程:

// save the initial value of the field so we know what the year is supposed to be set to
var initValue = document.getElementById("startDate").value;
var initValueSplit = initValue.split("/");

document.getElementById("startDate").onkeyup = function() {
    // get the value of whatever the user has entered
    var newValue = document.getElementById("startDate").value;
    // split the new value into an array, where 0th element is MM and 1st element is YYYY
    var newValueSplit = newValue.split("/");
    // rewrite the value of the field, using the user's version of the month and the saved version of the year
    var fixedValue = newValueSplit[0] + "/" + initValueSplit[1];
    // change the value of the field to fixedValue
    document.getElementById("startDate").value = fixedValue;
}

这将在用户尝试更改YYYY的值时将其更改回原始值/起始值。

您可以使用一些javascript代码来执行此操作,如()所示:

功能测试1(e,输入)
{
var key=e.keyCode | | e.charCode;
控制台日志(键);
如果(键==8 | |键==46)
{
e、 预防默认值();
e、 停止传播();
setCursorPosition(输入,(getCursorPosition(输入)-1));
返回false;
}
}
功能测试(e,输入)
{
控制台日志(“测试”);
e、 预防默认值();
e、 停止传播();
var tempString=input.value;
var cursorPosition=getCursorPosition(输入);
如果(光标位置!=2)
{
tempString=“”;
对于(变量i=0;i

这并不能真正回答问题。嗨,谢谢你的回答,这正是我想要的,但它不起作用。我已经在jQuery1.7.2中加载了代码,并将代码放在document.ready函数中。关于它为什么不起作用有什么想法吗?你能粘贴到你的输入html中吗?我更新了答案,包括了JSFIDLE是如何实现脚本的。id使用type
date
而不是
text
@Yasar如果你能使用bootstrap-datepicker插件,你的生活会更美好easier@YasarMahmood看看我的答案。我贴了一把小提琴,你可以试一试…@YasarMahmood让我知道这是你想要的还是你想要不同的东西?
<div id="startDate">
  <input id="startMonth" type="text" name="courseMonth" value="MM">/<input id="startYear" type="text" name="courseYear" value="YYYY">
</div>
// save the initial value of the field so we know what the year is supposed to be set to
var initValue = document.getElementById("startDate").value;
var initValueSplit = initValue.split("/");

document.getElementById("startDate").onkeyup = function() {
    // get the value of whatever the user has entered
    var newValue = document.getElementById("startDate").value;
    // split the new value into an array, where 0th element is MM and 1st element is YYYY
    var newValueSplit = newValue.split("/");
    // rewrite the value of the field, using the user's version of the month and the saved version of the year
    var fixedValue = newValueSplit[0] + "/" + initValueSplit[1];
    // change the value of the field to fixedValue
    document.getElementById("startDate").value = fixedValue;
}