Default.aspx前台代码<table style="width: 950px;">
<tr>
<td style="width: 250px; text-align: right">
用户:
</td>
<td style="width: 300">
<input type="text" id="txt_UserName" onblur="checkName()" /><br />
</td>
<td style="width: 400px; text-align: left">
<img id="i_username" src="" style="display: none" /><span id="a_UserName"></span>
</td>
</tr>
<tr>
<td style="width: 250px; text-align: right">
密码:
</td>
<td style="width: 300">
<input type="text" id="txt_Password" onkeyup="empty()" onblur="checkPassword()" /><br />
</td>
<td style="width: 400px; text-align: left">
<img id="i_password" src="" style="display: none" /><span id="a_Password"></span>
</td>
</tr>
</table>
JS代码var xmlHttp;
//用户名验证
function checkName()
{
createXMLHTTP();
var username = document.getElementById("txt_UserName").value;
xmlHttp.open("post","time.aspx",true);
xmlHttp.onreadystatechange = checkUserName;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("username="+username+"&action=cusername");
}
//密码验证
function checkPassword()
{
createXMLHTTP();
var password = document.getElementById("txt_Password").value;
xmlHttp.open("post","time.aspx",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.onreadystatechange=checkpassword;
xmlHttp.send("password="+password+"&action=cpassword");
} function createXMLHTTP()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlHttp)
{
window.alert("不能创建XMLHttpRequest对象实例!");
return false;
}
}
}
function checkUserName()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(xmlHttp.responseText=="true")
{
// document.getElementById("d_UserName").style.border="1px solid #B6D2E0";
document.getElementById("a_username").innerHTML="输入正确!";
document.getElementById("i_username").src="images/right.gif"
document.getElementById("i_username").style.display="";
}
else if(xmlHttp.responseText=="exist")
{
// document.getElementById("d_UserName").style.border="1px solid #B6D2E0";
document.getElementById("a_username").innerHTML="用户名已存在!";
document.getElementById("i_username").src="images/wrong.gif"
document.getElementById("i_username").style.display="";
}
else
{
// document.getElementById("d_UserName").style.border="1px solid #B6D2E0";
document.getElementById("a_username").innerHTML="用户名长度要大于4小于20!";
document.getElementById("i_username").src="images/wrong.gif"
document.getElementById("i_username").style.display="";
}
}
}
}
function checkpassword()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(xmlHttp.responseText=="true")
{
// document.getElementById("d_Password").style.border="1px solid #B6D2E0";
document.getElementById("a_password").innerHTML="输入正确!";
document.getElementById("i_password").src="images/right.gif";
document.getElementById("i_password").style.display = "";
}
else
{
// document.getElementById("d_Password").style.border="1px solid #B6D2E0";
document.getElementById("a_password").innerHTML="密码长度要大于4小于20!";
document.getElementById("i_password").src="images/wrong.gif";
document.getElementById("i_password").style.display = "";
}
}
}
}
然后time.aspx.cs后台代码 if (Request.Form["action"] == "cusername")
{
if (Request.Form["username"] != null)
{
string cmdstr = "select * from Users where User_Name='" + Request.Form["username"] + "'";
if (sqlhelp.CheckSql(cmdstr)) //判断用户是否存在
{
Response.Write("exist");
Response.End();
}
else if (Request.Form["username"].Trim().ToString().Length > 4 && Request.Form["username"].Trim().ToString().Length < 20)
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
else
{
Response.Write("false");
Response.End();
}
}
else if (Request.Form["action"] == "cpassword")
{
if (Request.Form["password"] != null)
{
if (Request.Form["password"].Trim().ToString().Length > 4 && Request.Form["password"].Trim().ToString().Length < 20)
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
else
{
Response.Write("no");
Response.End();
}
}
我想问的是
就是当我用户名和密码验证完后点击button后数据插入到数据库
如果用户名和密码验证不正确的话点button就会提示错误
<tr>
<td style="width: 250px; text-align: right">
用户:
</td>
<td style="width: 300">
<input type="text" id="txt_UserName" onblur="checkName()" /><br />
</td>
<td style="width: 400px; text-align: left">
<img id="i_username" src="" style="display: none" /><span id="a_UserName"></span>
</td>
</tr>
<tr>
<td style="width: 250px; text-align: right">
密码:
</td>
<td style="width: 300">
<input type="text" id="txt_Password" onkeyup="empty()" onblur="checkPassword()" /><br />
</td>
<td style="width: 400px; text-align: left">
<img id="i_password" src="" style="display: none" /><span id="a_Password"></span>
</td>
</tr>
</table>
JS代码var xmlHttp;
//用户名验证
function checkName()
{
createXMLHTTP();
var username = document.getElementById("txt_UserName").value;
xmlHttp.open("post","time.aspx",true);
xmlHttp.onreadystatechange = checkUserName;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("username="+username+"&action=cusername");
}
//密码验证
function checkPassword()
{
createXMLHTTP();
var password = document.getElementById("txt_Password").value;
xmlHttp.open("post","time.aspx",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.onreadystatechange=checkpassword;
xmlHttp.send("password="+password+"&action=cpassword");
} function createXMLHTTP()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlHttp)
{
window.alert("不能创建XMLHttpRequest对象实例!");
return false;
}
}
}
function checkUserName()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(xmlHttp.responseText=="true")
{
// document.getElementById("d_UserName").style.border="1px solid #B6D2E0";
document.getElementById("a_username").innerHTML="输入正确!";
document.getElementById("i_username").src="images/right.gif"
document.getElementById("i_username").style.display="";
}
else if(xmlHttp.responseText=="exist")
{
// document.getElementById("d_UserName").style.border="1px solid #B6D2E0";
document.getElementById("a_username").innerHTML="用户名已存在!";
document.getElementById("i_username").src="images/wrong.gif"
document.getElementById("i_username").style.display="";
}
else
{
// document.getElementById("d_UserName").style.border="1px solid #B6D2E0";
document.getElementById("a_username").innerHTML="用户名长度要大于4小于20!";
document.getElementById("i_username").src="images/wrong.gif"
document.getElementById("i_username").style.display="";
}
}
}
}
function checkpassword()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(xmlHttp.responseText=="true")
{
// document.getElementById("d_Password").style.border="1px solid #B6D2E0";
document.getElementById("a_password").innerHTML="输入正确!";
document.getElementById("i_password").src="images/right.gif";
document.getElementById("i_password").style.display = "";
}
else
{
// document.getElementById("d_Password").style.border="1px solid #B6D2E0";
document.getElementById("a_password").innerHTML="密码长度要大于4小于20!";
document.getElementById("i_password").src="images/wrong.gif";
document.getElementById("i_password").style.display = "";
}
}
}
}
然后time.aspx.cs后台代码 if (Request.Form["action"] == "cusername")
{
if (Request.Form["username"] != null)
{
string cmdstr = "select * from Users where User_Name='" + Request.Form["username"] + "'";
if (sqlhelp.CheckSql(cmdstr)) //判断用户是否存在
{
Response.Write("exist");
Response.End();
}
else if (Request.Form["username"].Trim().ToString().Length > 4 && Request.Form["username"].Trim().ToString().Length < 20)
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
else
{
Response.Write("false");
Response.End();
}
}
else if (Request.Form["action"] == "cpassword")
{
if (Request.Form["password"] != null)
{
if (Request.Form["password"].Trim().ToString().Length > 4 && Request.Form["password"].Trim().ToString().Length < 20)
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
else
{
Response.Write("no");
Response.End();
}
}
我想问的是
就是当我用户名和密码验证完后点击button后数据插入到数据库
如果用户名和密码验证不正确的话点button就会提示错误
解决方案 »
- 为什么页面会闪一下(有时间的大虾帮忙看下)
- HTML会取代wap吗?
- 求个ASP.net的存储过程分页,最好带显示页面的。谢谢(急求)
- 有关时间的SQL语句
- 急!如果在webform中调用word里面的公式编辑器!请大家指点。
- 大家都会遇到的一个问题:刷新页面的时候会重复执行上一次操作。例如对DB的插入,删除操作。
- 如何匹配并替换含有HTML标签的文档中关键字?
- 新人提问,请问我能把连接数据库写成一个类或方法,然后每次连接时调用它吗?大家能给出具体的代码例子吗?
- 30分问一个奇怪的问题(分不够,可另开贴给分)
- 请教众大虾:不使用字典转码,如何用.Net CRL实现汉字的简繁体编码转换!
- 关于密码的正则表达式问题
- 如何给formview中按钮加代码
该怎么解决
高手帮帮忙
OnClientClick是调用客户端的JS函数, 而OnClick用于调用后台服务器端的函数 OnClientClick先于OnClick执行 在JS判断出错误后加 return false;
是想接下去写不知道要怎么写
用户名和密码都用ajax验证好了
然后要写button的代码
要怎么写才能使当用户名和密码输入不正确的时候提示?
我用的是ajax客户端验证
要怎么做呢?
我用户名旁边不是有个div吗?如果输入错误那里会提示
如果输入错误就点击button的话
想用户名输入的那些字符还在,旁边那个提示也还在,就是点button如果输入错误什么也不执行,等输入正确的话就插入到数据库里
要怎么做呢?
function buttonClick() {
......//这里判断。
if(....){
$("#btnName").click();
}else{
return false;
}
}
{
if(!checkName())
{
alert("请输入正确的用户名!");
return false;
}
}
然后在button <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return checkName()" />
可是这样还是不行。
还是要重新判断用户名输入是否大于4小于20个字符?
如果在判断一次的话,那我写的那个ajax客户端好象有点多余了额?
该怎么办呢?
$("#formName").submit();
{
if(...) //判断为空 错误
{
return false;
}
}
function checkall()
{
if(...) //判断为空 错误
{
return false;
}
}
<asp:textbox onblur="checkName()" > //简略 <asp:button OnClientClick="return checkall()" > //简略 这样就OK
如果是这样的话我会
我想要用ajax客户端传到服务端在判断的
{
model.username = text1.Text.ToString().Trim();
bll.insert(model);
}
else
{
Lable1.Text = "请输入正确的用户名";
}
以上就可以,没有必要用你的复杂的方法
能尽量在客户端验证就在客户端验证,为什么非要在后台验证呢?
判断密码那些的话根本不用ajax
用js就可以了- -晕
只要用户名用ajax传到后台判断有没有重复就可以了- -
是这样的不?
if(tmpStr.length > 4 && tmpStr.length < 20 )
var regex = new RegExp("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if(regex.exec(email))
{
document.getElementById("i_email").style.display="";
document.getElementById("i_email").src="images/right.gif";
document.getElementById("a_email").innerHTML="电子邮箱格式正确!";
}
else
{
document.getElementById("i_email").style.display="";
document.getElementById("i_email").src="images/wrong.gif";
document.getElementById("a_email").innerHTML="电子邮箱格式不正确!";
}
写了这样不行额
接下来就是button了
我试下
||document.getElementById("a_username").innerHTML== "用户名长度要大于4小于20!")
return;
var Uname=document.getElementById("txt_UserName").value;
var Upwd=document.getElementById("txt_Password").value;
alert(_Default.LOk(Uname,Upwd).value);
}后台: [AjaxPro.AjaxMethod]
public string LOk(string Uname, string Upwd)
{
//你想判断什么就什么.....
if(插入成功)
return "ok";
else
return "sorry";
}