Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
子字符串javascript和html 函数myFunction(){ //我在2013年2月31日通过的示例 //var timeDate=document.getElementById('date')。文本;30) 警报(“无效日期”); var month2=时间日期子字符串(3,5); 控制台日志(month2); var monthArray=timeDate.split(“-”); console.log(monthArray[1]); var月=parseInt(monthArray[1],10); 控制台日志(月); 如果(月12) 警报(“无效月份”); }_Javascript_Html - Fatal编程技术网

子字符串javascript和html 函数myFunction(){ //我在2013年2月31日通过的示例 //var timeDate=document.getElementById('date')。文本;30) 警报(“无效日期”); var month2=时间日期子字符串(3,5); 控制台日志(month2); var monthArray=timeDate.split(“-”); console.log(monthArray[1]); var月=parseInt(monthArray[1],10); 控制台日志(月); 如果(月12) 警报(“无效月份”); }

子字符串javascript和html 函数myFunction(){ //我在2013年2月31日通过的示例 //var timeDate=document.getElementById('date')。文本;30) 警报(“无效日期”); var month2=时间日期子字符串(3,5); 控制台日志(month2); var monthArray=timeDate.split(“-”); console.log(monthArray[1]); var月=parseInt(monthArray[1],10); 控制台日志(月); 如果(月12) 警报(“无效月份”); },javascript,html,Javascript,Html,我的函数正在工作,只是我想做一些更正,比如用户输入 -23-11-2013/检查javascript子字符串函数 我的示例:结果为est <script> function myFunction(){ //Example I passed in 31-02-2013 //var timeDate = document.getElementById('date').text; <--This Wont Work! //This is some very basic example

我的函数正在工作,只是我想做一些更正,比如用户输入
-23-11-2013/检查javascript子字符串函数

我的示例:结果为
est

<script>
function myFunction(){
//Example I passed in 31-02-2013
//var timeDate = document.getElementById('date').text; <--This Wont Work!
//This is some very basic example only. Badly formatted user entry will cause all 
//sorts of problems.
var timeDate = document.getElementById('date').value;

//Get First 2 Characters 
var first2 = timeDate.substring(0,2);
console.log(first2);

var dateArray = timeDate.split("-");
console.log(dateArray[0]);

var date = parseInt(dateArray[0], 10) ;//Make sure you use the radix otherwise leading 0 will hurt
console.log(date);
if( date < 1 || date > 30 )
    alert( "Invalid date" );

var month2 = timeDate.substring(3,5);
console.log( month2 );

var monthArray = timeDate.split( "-" );
console.log( monthArray[1] );

var month = parseInt( monthArray[1],10 );
console.log( month );

if( month < 1 || month > 12 )
    alert( "Invalid month" );
}
</script>
试试这个

var日期=“-23-11-2013”

数据=日期。拆分(“-”)

如果(data.length==3){

如果(数字(数据[0])>31){

警报(“无效日期格式”)

}否则如果(数字(数据[1])>12){

警报(“无效的月份格式”)

} }否则{

警报(“格式不正确”)


}他是一个更好的功能,您或许可以使用:

var a ="test";
console.log(a.substring(1));
它应该返回无效日期日期对象。

因此像
myFunc(“23-11-2013”)
myFunc(“23-11-2013”)
这样的调用应该返回一个日期对象:

function myFunc(s) {

   s = s.split("-").filter(Number); 

   return new Date(s[2], s[1], s[0]);
}

您可以使用以下更好的功能:

Mon Dec 23 2013 00:00:00 GMT+0530 (India Standard Time)
函数myFunction(日期){
var args=date.split(/[^0-9]+/),
i、 l=args.长度;
//准备args
对于(i=0;i 12){
抛出新错误(“无效月份”);
}
//检查日(从第0天到上个月最后一天构造函数返回的日期)
if(args[0]>新日期(args[2],args[1],0).getDate(){
抛出新错误(“无效日期”);
}
返回新日期(args[2],args[1]-1,args[0]);
}
注意
Date
构造函数中的月份是基于
0
的,您需要从实际值中减去
1
。除此之外,由于不同的月份有不同的天数,所以你有错误的日期检查。提供的函数还允许传递带有空格和特殊字符的值,如
-23-11/2013
,唯一重要的是数字的顺序(日、月、年)


在这里,您可以看到它正在工作

您可以创建此功能的小提琴吗?您希望函数具体做什么?只需在输入字符串的开头去掉一个可能的连字符?
function myFunction(date) {
  var args = date.split(/[^0-9]+/),
      i, l = args.length;

  // Prepare args
  for(i=0;i<l;i++) {
    if(!args[i]) {
      args.splice(i--,1);
      l--;
    } else {
      args[i] = parseInt(args[i], 10);
    }
  }

  // Check month
  if(args[1] < 1 || args[1] > 12) {
    throw new Error('Invalid month');
  }
  // Check day (passing day 0 to Date constructor returns last day of previous month)
  if(args[0] > new Date(args[2], args[1], 0).getDate()) {
    throw new Error('Invalid date');
  }

  return new Date(args[2], args[1]-1, args[0]); 
}