var dt = new Date();var str = "your date string here";
var re = /^(\d{4})-(\d{1,2})-\d{1,2}$/;
if (re.test(str))
{
  var nYear = parseInt(RegExp.$1);
  var nMonth = parseInt(RegExp.$2);
  if (nYear == dt.getFullYear() && (nMonth == (dt.getMonth()+1) || nMonth == dt.getMonth()))
    somethingForm.submit();
}

解决方案 »

  1.   

    非常感谢!
    var str = "your date string here";   ???
    当前日期所在月是变量,要用JS取得当前日期!
    不用正则表达式和用正则表达式各怎么做?主要目的是取得当前系统时间,并和用户在text框填入的日期进行比较,如果填入日期就是当前月或者当前月的上一个月,就允许提交,否则不允许提交并提示非法输入。
      

  2.   

    <form onsubmit="return(check(d.value))">
    <input name=d>
    </form>
    <script>
    function check(str){
    var dt = new Date();
    var reg = /^(\d{4})-(\d{1,2})-\d{1,2}$/;
    if(arr=str.match(reg))
    return (arr[1] == dt.getFullYear() && (arr[2] == (dt.getMonth()+1) || arr[2] == dt.getMonth()))
    else
    return false;
    }
    </script>
      

  3.   

    var dt = new Date();
    当前日期所在月: dt.getMonth()+1
      

  4.   

    如果当前月份是一月呢所以建议用时间差,改为<form onsubmit="return(check(d.value))">
    <input name=d>
    </form>
    <script>
    function check(str){
    var dt = new Date();
    var reg = /^(\d{4})-(\d{1,2})-\d{1,2}$/;
    if(arr=str.match(reg)){
    if(arr[1] == dt.getFullYear() && arr[2] == (dt.getMonth()+1))return true;
    dt.setDate(0);
    if(arr[1] == dt.getFullYear() && arr[2] == (dt.getMonth()+1))return true;
    }
    alert("error");
    return false;
    }
    </script>