JQuery USES ajax registered user instance of background asp.net
- 2020-05-07 19:28:24
- OfStack
//JS code
function checkname() {
var Msg = document.getElementById("d_username");
var chk = CheckN();
if (chk) {
$.ajax({ url: 'registerOK.aspx',
type: 'POST',
data: { lb: "checkname", username: $("#username").val() },
dataType: 'html',
timeout: 1000,
error: function() { alert(' An unknown error has occurred, please contact QQ:6434789 Contact. '); },
success: function(data) {
if (data == " Sorry, this username has been registered! ") {
Msg.className = "d_err";
Msg.innerHTML = data;
$("#hiddencheckname").val("no");
}
if (data == " Congratulations, this username can be registered! ") {
Msg.className = "d_ok";
Msg.innerHTML = data;
$("#hiddencheckname").val("yes");
}
if (data == " This user name cannot be Chinese! ") {
Msg.className = "d_err";
Msg.innerHTML = data;
$("#hiddencheckname").val("no");
}
if (data == " Please enter user name! ") {
Msg.className = "d_err";
Msg.innerHTML = data;
$("#hiddencheckname").val("no");
}
}
});
}
}
function CheckN() {
var Msg = document.getElementById("d_username");
var chk = true;
if (checkIsChinese(document.getElementById("username").value)) {
Msg.className = "d_err";
Msg.innerHTML = " User name cannot be Chinese! ";
chk = false;
}
if (document.getElementById("username").value.length < 4) {
Msg.className = "d_err";
Msg.innerHTML = " The username cannot be less than 4 A character! ";
chk = false;
}
return chk;
}
function chk_reguser() {
var chk = true
// if (!checkname()) { chk = false; } //jquery Variable assignment cannot be matched js The variables inside are Shared, so success: function(data) {} Any variables defined in it are associated with the js The variables in the code are not interchangeable, so I look for them 1 A more informal (informal) approach, as in html Add hidden Input And in the js It set up $("#hiddencheckname").val("no");
if (!CheckN()) {chk = false;}
if ($("#hiddencheckname").val() == "no") { chk = false; }// This is a judgment on it
if (!checkpwd()) { chk = false; }// These with CheckN() almost 1 Sample, just to verify the elements in the form
if (!checkpwd1()) { chk = false; }
if (!checkquestion()) { chk = false; }
if (!checkanswer()) { chk = false; }
if (!checkxq()) { chk = false; }
if (!checkrealname()) { chk = false; }
if (!checktel()) { chk = false; }
if (!checkQQMSN()) { chk = false; }
if (!checkEmail()) { chk = false; }
if (!checkaddress()) { chk = false; }
if (chk) {
$.ajax({ url: 'registerOK.aspx',
type: 'POST',
data: { lb: "reguser", username: $("#username").val(), password: $("#pwd").val(), question: $("#question").val(), answer: $("#answer").val(), xqcode: $("#ctl00_CPH_Main_DDL_xq").val(), realname: $("#realname").val(), sex: $("input[@name=sex][@checked]").val(), tel: $("#tel").val(), QQMSN: $("#QQMSN").val(), email: $("#email").val(), address: $("#address").val() },
dataType: 'html',
timeout: 1000,
error: function() { alert(' An unknown error has occurred, please contact QQ:6434789 Contact. '); },
success: function(data) {
if (data == "success") {
alert(" Registration successful! ");
window.location.href = "/member/index.aspx";
}
else {
alert(" Registration failed, the reason may be that the user name has been registered! ");
}
}
});
return (true);
}
return (false);
}
/ / HTML page
Just do a form like this
<input id="username" type="text" onBlur="checkname()" />
<div class=d_default id=d_username> Please enter the 4 English characters or Numbers with more than one digit! </div>
<input id="Button_OK" type="button" onclick="return chk_reguser();"/>
//registerOK.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
public partial class registerOK : System.Web.UI.Page
{
user user = new user();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string lb = Request["lb"];
string username = Request["username"];
string password = Request["password"];
string question = Request["question"];
string answer = Request["answer"];
string xqcode = Request["xqcode"];
string realname = Request["realname"];
string sex = Request["sex"];
string tel = Request["tel"];
string QQMSN = Request["QQMSN"];
string email = Request["email"];
string address = Request["address"];
if (lb == "checkname")
{
Response.Write(user.CheckUserName(username));
}
if (lb == "reguser")
{
string name = user.CheckUserName(username);
if (name == " Congratulations, this username can be registered! ")
{
user.Add(username, password, question, answer, xqcode, realname, sex, tel, QQMSN, email, address);
Response.Write("success");
}
else
{
Response.Write("fail");
}
}
}
}
}
Note: the registerOK.aspx page must be cleared of all HTML code, if only < %@ Page Language="C#" AutoEventWireup="true" CodeFile="registerOK.aspx.cs" Inherits="registerOK" % > .