检查“文本框"的值是否为“日期". [英] Check Textbox value is Date or not.

查看:80
本文介绍了检查“文本框"的值是否为“日期".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,每个人,

我正在使用c#.我有一个文本框,其中添加了ajax的压延扩展器.
我想检查文本框中的输入值是日期还是任何简单的文本.

但是我不知道将使用什么代码?

请帮帮我.

我想验证是否在文本框中输入的值是日期?

Hello Every one,

I am using c#. i have textbox in which i add calender extender of ajax.
I want to check that enter value in textbox is date or any simple text.

But i don''t know what code will be used?

Please help me.

I want to validate that value entered in textbox is date or not?

推荐答案

此处是您在按钮中输入的内容:
Here is what you put in the button:
<input type="submit" id="btnSubmit" value="Submit" onClick="return Validate();"/>


JavaScript


Javascript

<script type="text/javascript">
<!--

 function Validate()
 {

  var intFlag = 0;
  var strErrMsg = "Please complete the following field(s):\n\n";

  var dtDate= document.getElementById("tbDate").value; // tbDate = name of text box
  var currentDate= getCalendarDate()


  if (dtDate > currentDate )
  {
   strErrMsg = strErrMsg + "your custom error message \n";
   intFlag++;
  }

    // Display error message if a field is not completed
  if (intFlag != 0)
  {
   alert (strErrMsg);
   return false;
  }
  else
   return true;

 }

//-->
</script>


在c#

您可以使用
Convert.ToDateTime(txtDate.Text)
尝试-catch语句

如果出现错误,则可以空白文本框

in c#

you can use
Convert.ToDateTime(txtDate.Text)
with in a try - catch statement

if error then you can blank textbox

try
{
    txtDt.Text = Convert.ToDateTime(txtDt.Text).ToString("dd-MMM-yyyy");
}
catch (Exception eee)
{
    txtDt.Text = "";
}
if (txtDt.Text.Trim() == "")
{
    ClientScript.RegisterStartupScript(GetType(), ClientID, "alert('Enter Valid Date');", true);
    return;
}


如果您想在服务器端进行操作,可以尝试一下

DateTime curDate = DateTime.Now;
如果(DateTime.TryParse(ToDate,out curDate))
{
\\您的代码在这里
}

希望这会有所帮助.
You can try this, if you want to do it at Server Side

DateTime curDate = DateTime.Now;
if (DateTime.TryParse(ToDate, out curDate))
{
\\Your Code goes here
}

Hope this helps.


这篇关于检查“文本框"的值是否为“日期".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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