如何在asp.net中进行验证.....# [英] How to make validations.....in asp.net c#
问题描述
亲爱的朋友们,
我使用c#Sqlserver 2005在asp.net中创建了一个网页。
在这里我有文本框和提交按钮将数据保存在数据库中。
但是在这里,我需要在这里设置验证
我有3个字段作为电话主页,电话局,MobileNo
这里用户可以输入3个文本框输入
用户可以输入2个文本框,
但3个文本框应该不要空,至少一个应该填空
请你能帮帮我,怎么做。
提前致谢
Dear Frnds,
I have created a web page in asp.net using c# Sqlserver 2005.
In this i have textboxes and a submit button to save the data in database.
But here, i need to set validations here
I have 3 fields as Telephone Home, Telephone Office, MobileNo
Here User can enter in enter in 3 textboxes
user can enter in 2 textboxes,
but 3 textboxes should not be empty, at least one should filled
Please can you help me, how to do this.
Thanks in advance
推荐答案
你好我已经创建了简单的代码,你可以尝试这个 -
hello i have created simple code ,u can try this -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
c#code -
c# code -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "")
{
Label1.Text = "mobile number does not save";
}
else
{
Label1.Text = "save ur mobile number ";
}
}
}
如果所有文字框都是如果任何文本框有一些值,移动号码将成功保存,则显示移动号码不会保存。
if all text box null than it show mobile number does not save otherwise if any textbox have some value than mobile number will be successfully saved.
这里我用java做了脚本工作正常。检查这个在头部添加轰鸣声脚本
Hi,
Here i did with java script working fine.Check this add the bellow script in head section
<script type="text/javascript">
<!--
function validation()
{
if (document.getElementById("txtPhoneHome").value == "" && document.getElementById("txtPhoneOffice").value == ""&&document.getElementById("txtMobile").value == "")
{
alert('Please enter atleast one contact no.');
document.getElementById("txtMobile").focus();
return false;
}
}
-->
</script>
和这里的html表格
and here html form
<asp:textbox id="txtPhoneHome" runat="server" onkeypress="return clickButton(event,'btnSubmit')"></asp:textbox>
<asp:textbox id="txtPhoneOffice" runat="server" onkeypress="return clickButton(event,'btnSubmit')"></asp:textbox>
<asp:textbox id="txtMobile" runat="server" onkeypress="return clickButton(event,'btnSubmit')"></asp:textbox>
<asp:button id="btnSubmit" runat="server" text="Submit" onclientclick="return validation();" />
使用自定义验证功能并使用此属性
ClientValidationFunction =Validate
use custom validation funuction and use this property
ClientValidationFunction="Validate"
function Validate(sender, args) {
var txt1 = document.getElementById("txtphoneh");
var txt2 = document.getElementById("txtphonew");
var txt3 = document.getElementById("txtphonec");
args.IsValid = ((txt1.value != "") || (txt2.value != "") || (txt3.value != ""));
}
这篇关于如何在asp.net中进行验证.....#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!