使用java脚本的问题.. [英] Problen with java script..

查看:69
本文介绍了使用java脚本的问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下javascript函数来验证日期。

I am using following javascript function to validate date.

function IsValidDate(FormName,ElemName, err_display) 
{
	var ret = true;
	var dateStr;
	if( ElemName != "" )
	{
		if( Elem( FormName, ElemName ) == null ) return false;
		dateStr = Elem( FormName, ElemName ).value;
			
	}
	else
	{
		dateStr = FormName;
	}
		
	if (dateStr != "")
	{
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|[1-9][0-9][0-9][0-9])$/;
		var matchArray = dateStr.match(datePat); 
		if (matchArray == null) 
		{
			if( err_display != "" ) 
			{
				alert(err_display + " is not in a valid format.")
				 FocusElement( FormName, ElemName);
			}
			ret = false
			return false;
		}
		month = matchArray[1]; 
		day = matchArray[3];
		year = matchArray[4];
		dt = new Date();
		yearchk = dt.getFullYear();
		if (month < 1 || month > 12)
		{ 
			if( err_display != "" ) 
			{
				alert( err_display + ": Month must be between 1 and 12.");
				 FocusElement( FormName, ElemName);
					
			}
			ret = false
			return false;
				
		}
		if (day < 1 || day > 31) 
		{
			if( err_display != "" ) 
			{
				alert( err_display +  ": Day must be between 1 and 31.");
				 FocusElement( FormName, ElemName);
			}
			ret = false
			return false;
			
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
			if( err_display != "" ) 
			{
				alert( err_display +  ": Month "+month+" doesn't have 31 days.")
				 FocusElement( FormName, ElemName);
			}
			ret = false
			return false
				
		}
		if (month == 2) 
		{ 
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) 
			{
				if( err_display != "" ) 
				{
					alert( err_display +  ": February " + year + " doesn't have " + day + " days.");
					 FocusElement( FormName, ElemName);
				}
				ret = false
				return false;
					
			}
		}
    if (year <= 1900 && err_display != "")
    {
      alert(err_display + ": year must be greater 1900.");
      FocusElement( FormName, ElemName);
      ret = false
      return false
    }
		return true;
	}
	if( ret == false )
	{
		if( err_display != "" ) 
		{ 
			alert(err_display + " is not in a valid format.");
			FocusElement( FormName, ElemName);
		}
		else
		{
			alert("Date is not in a valid format." );
			 FocusElement( FormName, ElemName);
		}
		return false;
	}
}





问题

这个如果验证单个日期,函数工作正常..

双文本框出现问题..我有两个文本框和一个按钮.on客户端点击butten事件我调用isValidDate()函数两次下面的文本框..



< asp:按钮ID =btnGetReportrunat =服务器Text =获取报告OnClick =btnGetReport_Click OnClientClick =返回IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateFrom'',''开始日期'');返回IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateTo'',''结束日期''); />







这里的函数仅检查txtDateFrom文本框而不是txtDateTo文本盒子。如何解决这个问题。我必须使用上面的功能检查单个事件的两个文本框。



i认为由于返回语句而出现问题,cozz first函数返回一些东西并终止。

请帮助.... !!



Problem
This function works fine if validate single date..
Problem arise with double text box ..I have two textbox and a button .on client click of butten event i am calling isValidDate() function two times for both text box like below..

<asp:Button ID="btnGetReport" runat="Server" Text="Get Report" OnClick="btnGetReport_Click" OnClientClick = "return IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateFrom'', ''Start date'');return IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateTo'', ''End date'');"/>



Here function checks only txtDateFrom text box not txtDateTo text box ..How to solve this problem. I have to check both text box on single event by using above function.

i think problem arisng due to return statement, cozz first function returns something and terminated.
Please help....!!

推荐答案

/;
var matchArray = dateStr.match(datePat);
if (matchArray == null
{
if (err_display!=
{
alert(err_display + 的格式不正确。
FocusElement(FormName,ElemName);
}
ret = false
return ;
}
month = matchArray [ 1 ];
day = matchArray [ 3 ];
year = matchArray [ 4 ];
dt = new 日期();
yearchk = dt.getFullYear();
if (月< 1 || month> 12
{
if (err_display!=
{
alert(err_display + :月份必须介于1到12之间。);
FocusElement(FormName,ElemName);

}
ret = false
return < span class =code-keyword> false
;

}
if (day< 1 || day > 31
{
if (err_display!=
{
alert(err_display + :日期必须介于1到31之间。);
FocusElement(FormName,ElemName);
}
ret = false
return ;

}
if ((month == 4 ||月== 6 || month == 9 || month == 11 )&& day == 31
{
if (err_display!=
{
alert(err_display + :月 +月+ 没有31天。
FocusElement(FormName,ElemName);
}
ret = false
return false

}
if (month == 2
{
var isleap =(年% 4 == 0 &&(年% 100 != 0 ||年% 400 == 0 ));
if (day> 29 ||(day == 29 &&!isleap ))
{
if (err_display!=
{
alert(err_display + :二月 +年+ 没有 + day + days。);
FocusElement(FormName,ElemName);
}
ret = false
return ;

}
}
如果(年< = 1900 && err_display!=
{
alert(err_display + :year必须大于1900。);
FocusElement(FormName,ElemName);
ret = false
return false
}
return true ;
}
if (ret == false
{
if (err_display!=
{
alert(err_display + 格式不正确。< /跨度>);
FocusElement(FormName,ElemName);
}
其他
{
alert( 日期格式不正确。);
FocusElement(FormName,ElemName);
}
返回 false ;
}
}
/; var matchArray = dateStr.match(datePat); if (matchArray == null) { if( err_display != "" ) { alert(err_display + " is not in a valid format.") FocusElement( FormName, ElemName); } ret = false return false; } month = matchArray[1]; day = matchArray[3]; year = matchArray[4]; dt = new Date(); yearchk = dt.getFullYear(); if (month < 1 || month > 12) { if( err_display != "" ) { alert( err_display + ": Month must be between 1 and 12."); FocusElement( FormName, ElemName); } ret = false return false; } if (day < 1 || day > 31) { if( err_display != "" ) { alert( err_display + ": Day must be between 1 and 31."); FocusElement( FormName, ElemName); } ret = false return false; } if ((month==4 || month==6 || month==9 || month==11) && day==31) { if( err_display != "" ) { alert( err_display + ": Month "+month+" doesn't have 31 days.") FocusElement( FormName, ElemName); } ret = false return false } if (month == 2) { var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day>29 || (day==29 && !isleap)) { if( err_display != "" ) { alert( err_display + ": February " + year + " doesn't have " + day + " days."); FocusElement( FormName, ElemName); } ret = false return false; } } if (year <= 1900 && err_display != "") { alert(err_display + ": year must be greater 1900."); FocusElement( FormName, ElemName); ret = false return false } return true; } if( ret == false ) { if( err_display != "" ) { alert(err_display + " is not in a valid format."); FocusElement( FormName, ElemName); } else { alert("Date is not in a valid format." ); FocusElement( FormName, ElemName); } return false; } }





问题

这个如果验证单个日期,函数工作正常..

双文本框出现问题..我有两个文本框和一个按钮.on客户端点击butten事件我调用isValidDate()函数两次下面的文本框..



< asp:按钮ID =btnGetReportrunat =服务器Text =获取报告OnClick =btnGetReport_Click OnClientClick =返回IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateFrom'',''开始日期'');返回IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateTo'',''结束日期''); />







这里的函数仅检查txtDateFrom文本框而不是txtDateTo文本盒子。如何解决这个问题。我必须使用上面的功能检查单个事件的两个文本框。



i认为由于返回语句而出现问题,cozz first函数返回一些东西并终止。

请帮助.... !!



Problem
This function works fine if validate single date..
Problem arise with double text box ..I have two textbox and a button .on client click of butten event i am calling isValidDate() function two times for both text box like below..

<asp:Button ID="btnGetReport" runat="Server" Text="Get Report" OnClick="btnGetReport_Click" OnClientClick = "return IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateFrom'', ''Start date'');return IsValidDate(''aspnetForm'',''ctl00_ContentPlaceHolderBody_txtDateTo'', ''End date'');"/>



Here function checks only txtDateFrom text box not txtDateTo text box ..How to solve this problem. I have to check both text box on single event by using above function.

i think problem arisng due to return statement, cozz first function returns something and terminated.
Please help....!!


尝试使用这样的方法,而不是调用两种方法。



Try to use like this, instead of calling two methods.

<asp:Button ID="btnGetReport" runat="Server" Text="Get Report" OnClick="btnGetReport_Click" OnClientClick = "return IsValidDateDateFuncions();/>





<script>
function IsValidDateDateFuncions()
{
    var TestDate1=return IsValidDate('aspnetForm','ctl00_ContentPlaceHolderBody_txtDateTo', 'End date');

    var TestDate2=return IsValidDate('aspnetForm','ctl00_ContentPlaceHolderBody_txtDateFrom', 'Start date')
if(TestDate1 && TestDate2)
{
return true;
}
}

</script>


更改此代码:

Change the code like this:
<asp:button id="btnGetReport" runat="Server" text="Get Report" onclick="btnGetReport_Click" onclientclick="return CheckValidDates();" xmlns:asp="#unknown" />




function CheckValidDates()
{
if(IsValidDate('aspnetForm','ctl00_ContentPlaceHolderBody_txtDateFrom', 'Start date') == true && IsValidDate('aspnetForm','ctl00_ContentPlaceHolderBody_txtDateTo', 'End date')== true)
return true;
else
return false;
}


这篇关于使用java脚本的问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆