How to do this validation in asp.net?

mylittlejava 2005-01-18 01:02:10
I am new on asp.net programming.
Here I got a problem about validator
I want to write a login.aspx page, for example, there is UserName box. I need to validate two things on UserName box,
1. It must be filled. If it is empty, I will show a red message right after this box, like "UserName is empty!"
2. It must be a valid user in our database. So on server side, we will check this UserName in our database. If it is not in our database, we will show something like "Invalid user name..."
I know it is very easy to do the 1st rule with the RequireFieldValidator. But in order to do the rule 2, I need to put a Lable at the same place where the validator is. Right? But the two won't stay at the same place. So I tried to use a CustomValidator, and write the ClientValidationFunction like this:
<script language="javascript">
function ValidUserName(oSrc, args) //For rule 1
{
if(args.Value.length > 0)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
And also, I have a server validate function which will process the rule 2.
But the problem is the client validate function never get called. So , do you guys have a better solution for this? I really appreciate.
...全文
54 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Overriding 2005-01-18
  • 打赏
  • 举报
回复
CustomVlidator
beibeilong 2005-01-18
  • 打赏
  • 举报
回复
例如你在btLogin的后台代码里面写了第二个验证的话,你可以在page_load的时候写
btLogin.Attributes.Add("onclick","return ValidUserName()");,同时你的ValidUserName函数需要修改一下return的值。这样按下按钮后,会先执行ValidUserName(),如果返回true接着执行后台代码验证rule 2

62,056

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧