服务器端验证(c#asp.net)

服务器端验证(c#asp.net)

问题描述:

如何为空控件应用服务器端验证??

例如:-

我有一个ID为"txtusername"的文本框..
我想对此控件的空值应用验证..


我可以单击提交"按钮进行检查..


就像if(txtusername.text.trim()== string.Empty)
{
(应该在此处编写什么代码?)
}

我想在弹出窗口中显示一条消息,例如请输入用户名"
多谢!!! :)
也可以,如果您也可以帮助告诉我其他验证信息..
(例如有效的日期时间,电子邮件地址,整数值等...)

How to apply server side validation for an empty control???

for e.g:-

i have a textbox having id "txtusername"..
i want to apply validation for the empty value for this control..


i can check it on clicking the submit button..


like if(txtusername.text.trim()==string.Empty)
{
(what code should be written here?)
}

i want to show a message in a popup window like "Please enter username"
thanks a lot!!! :)
also if u can help tell me for the other validations too..
(like for valid datetime,email address,integer value etc...)



为什么必须在服务器端而不是客户端站点上启动验证. :)
无论如何都可以使用
Hi,

Why you have to fire validation on server side instead of client site. :)
anyway you can use
if(txtusername.text.Trim()==string.Empty)
{
 Response.Write("<script>alert('User should be required!');</script>");
}




谢谢,
Imdadhusen




Thanks,
Imdadhusen


请放开对方所说的话,您总是可以在服务器上进行验证,可以篡改客户端代码.如果您有一个验证摘要,我相信您可以直接在代码中使其无效,并使其显示您想要的任何消息.或者,您可以插入脚本以显示弹出窗口,但我认为这很丑陋,尤其是如果可能有多个无效项.
Desite what the other poster said, you sould ALWAYS validate on the server, client side code can be tampered with. If you have a validation summary, I believe you can make it invalid directly in code, and get it to show whatever message you want. Or you can insert script to show a popup, but I think that''s pretty ugly, esp if there could be more than one item that is invalid.


如果不这样做,请使用RequiredFieldValidator不想允许使用空/空数据.您可以使用不同的选项将错误显示给用户.您可以选择提醒用户.

默认情况下,验证是在客户端和服务器端进行的.
在服务器端,您必须编写:
Use RequiredFieldValidator if you don''t want to allow null/empty data. U hav different options how the error is displayed to the user. You can select to alert user.

By default the validation is done at client side as well as server side.
At server side u have to write:
if(Page.IsValid)<br />
{<br />
    //do watevr u want to do<br />
}


这将在button_click事件中.

因此,如果在客户端的浏览器上禁用了javascript,服务器端验证也将完成,并且不会输入无效的条目
可以参考这些以获取更多详细信息:
如何:使用RequiredFieldValidator控件 [ ASP.NET RequiredFieldValidator控件 [


This will be inside button_click event.

Thus in case if javascript is disabled at client''s browser, server side validation will also be done and invalid entries won''t be entered
May refer these for more details:
HOW TO: Use the RequiredFieldValidator Control[^]
ASP.NET RequiredFieldValidator Control[^]