Copy Code code as follows:
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 occurred, please contact qq:6434789. '); },
Success:function (data) {
if (data = = "Sorry, the username has been registered!") ") {
Msg.classname = "D_err";
msg.innerhtml = data;
$ ("#hiddencheckname"). Val ("no");
}
if (data = = Congratulations, the user name can be registered!) ") {
Msg.classname = "D_OK";
msg.innerhtml = data;
$ ("#hiddencheckname"). Val ("yes");
}
if (data = = The 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 = "User name cannot be less than 4 characters!" ";
CHK = false;
}
return chk;
}
function Chk_reguser () {
var chk = True
if (!checkname ()) {chk = false;}//jquery variable assignment cannot be shared with the variables in JS, so any variables defined in success:function (data) {} are not connected to the variables in the JS code in which they are located. So I'm looking for a rather partial (informal) way to add hidden input to HTML and set $ ("#hiddencheckname") in JS. Val ("no");
if (! Checkn ()) {chk = false;}
if ($ ("#hiddencheckname"). val () = = "No" {chk = false;} This is the judge of it.
if (!checkpwd ()) {chk = false;} These are the same as checkn (), just validating the elements inside 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 occurred, please contact qq:6434789. '); },
Success:function (data) {
if (data = = "Success") {
Alert ("Registered successfully!") ");
Window.location.href = "/member/index.aspx";
}
else {
Alert ("Registration failed because the user name may have been registered!") ");
}
}
});
return (true);
}
return (false);
}
HTML page
Just make a form like this
Copy Code code as follows:
<input id= "username" type= "text" onblur= "CheckName ()"/>
<div Class=d_default id=d_username> Please enter more than 4 digits in English characters or numbers! </div>
<input id= "BUTTON_OK" type= "button" onclick= "return Chk_reguser ();" />
RegisterOK.aspx.cs
Copy Code code as follows:
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, the user name 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 clear all HTML code, such as only the <%@ page language= "C # autoeventwireup=" true "codefile=" RegisterOK.aspx.cs "Inherits=" Registerok "%>.