VB.NET WEB页面中,有一个Textbox框,我让用户输入的是2008/12/06 类似的日期,如何判断用户输入的正确性!
VB.NET WEB页面中,有一个Textbox框,我让用户输入的是2008/12/06 类似的日期,如何判断用户输入的正确性! 如果不正确,则给出一个提示,并让光标仍然停留在textbox中并全选.请问如何做呢? 谢谢! --------------------编程问答-------------------- 你这样很难验证,情况太多了,为何不用时间控件呢。AjaxToolkit中的Canlender控件,可以设定时间格式,
不用自己验证了,而且很容易设置。 --------------------编程问答-------------------- 用CompareValidator验证控件!! --------------------编程问答-------------------- 验证控件方便些 --------------------编程问答-------------------- 这是一个例子:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
using System;
public partial class _Default : System.Web.UI.Page
{
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
try
{
DateTime dt = DateTime.Parse(TextBox1.Text);
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" +
ex.Message.Replace("'", "\\'").Replace("\r\n", "\n") + "');", true);
this.TextBox1.Focus();
}
}
}
当你使用UpdataPanel时,基本上无需修改代码(可以考虑将Page.ClientScript换成ScriptMamager)。 --------------------编程问答-------------------- 有没有VB.NET版本的? --------------------编程问答-------------------- 用验证控件喽。JS也可以!
验证控件分VB,C#? --------------------编程问答-------------------- TO: sp1234 可以用VB.NET吗?
补充:.NET技术 , ASP.NET