实现手机发送验证码 进行验证
Html:
<li>
<label class="k" for="">
手机号码:</label>
<span class="v">
<input class="psp-text" style="width: 90px; ime-mode: disabled;" type="text" id="txtMobileCode"
name="txtMobileCode" maxlength="11" size="11" onpaste="return false" />
<input type="button" value="获取验证码" onclick="SetCode(true);" id="btnCode" />
</span><em id="tipTel"></em></li>
jquery:
function SetCode(n) {
if ($('#txtMobileCode').length = 11) {
if (n)
$.ajax({
type: "get",
dataType: "json",
url: "ajax.aspx?oper=telpcode&clienttime=" + (new Date().getTime()),
data: "txtMobileCode=" + $("#txtMobileCode").val(),
success: function (d) {
if (d.result == "1") {
alert("验证码发送成功");
GetNumber();
return true;
}
else {
alert("验证码发送失败");
return false;
}
}
});
else { }
}
else {
}
}
var count = 5;
function GetNumber() {
$("#btnCode").attr("disabled", "disabled");
$("#btnCode").val(count + "秒之后点击获取")
count--;
if (count > 0) {
setTimeout(GetNumber, 1000);
}
else {
$("#btnCode").val("点击获取验证码");
$("#btnCode").attr("disabled", "");
count = 5;
}
}
AjAX.aspx CS:
private void ajaxTelpCode() //产生验证码
{
sms.Service sms = new sms.Service();//短信服务接口
if (q("txtMobileCode") != "")
{
string telp = q("txtMobileCode");//页面电话号码
Random random = new Random();
string telcode = random.Next(100000, 999999).ToString();//6位验证码
string s = sms.mt("xmysy", "fff", telp, "您的验证码为:" + telcode).ToString();//接口发送
if (s == "0")
{
xmlDoc.Load(HttpContext.Current.Server.MapPath("~/TelCode.xml"));//定义xml文件 来保存验证码和手机好
XmlNode root = xmlDoc.SelectSingleNode("Tcode");
XmlNodeList nodelist = xmlDoc.SelectSingleNode("Tcode").ChildNodes;
for (int i = 0; i < nodelist.Count; i++)//读取原来验证XMl文件
{
XmlElement xe = (XmlElement)nodelist.Item(i);
if (xe.GetAttribute("tel") == telp)//判断手机是否已存在 存在将其删除 保证验证码唯一并是最新的、、【重新发送】
{
root.RemoveChild(xe);
if (i < nodelist.Count)
i = i - 1;
}
}
//新增一笔记录 保存手机号与对应的验证码
XmlElement xe1 = xmlDoc.CreateElement("Node");
xe1.SetAttribute("tel", telp);
XmlElement xesub1 = xmlDoc.CreateElement("Code");
xesub1.InnerText = telcode;
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("Tel");
xesub2.InnerText = telp;
xe1.AppendChild(xesub2);
root.AppendChild(xe1);
xmlDoc.Save(HttpContext.Current.Server.MapPath("~/TelCode.xml"));
this._response = JsonResult(1, "验证码发送成功");//返回内容
}
else
{
this._response = JsonResult(0, "验证码发送失误");
}
}
else
this._response = JsonResult(0, "为空");
}
private void ajaxRegister()//注册按钮
{
string _code = f("txtCode");
string _tel = f("txtMobileCode");
bool istrue = true;
DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("~/TelCode.xml"));
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["Code"].ToString() == _code && dt.Rows[i]["Tel"].ToString() == _tel)
{
istrue = false;
break;
}
else
{
}
}
}
if (istrue)
{
Response.Write("JumboTCMS.Alert('验证码错误', '0');");
Response.End();
}
........
}
XML :
<Tcode>
<Node tel="15980908000">
<Code>696009</Code>
<Tel>15980908000</Tel>
</Node>
<Node tel="15980908001">
<Code>689998</Code>
<Tel>15980908001</Tel>
</Node>
</Tcode>