標籤:
<script type="text/javascript">
function username() {
$(‘#msg1‘).remove();
var username = $("#Username").val();
var reg = new RegExp("^([A-Z a-z u4E00-u9FA5]{4,16})$");
if (username == null || username == "") {
$(‘#Username‘).after("<b id=‘msg1‘ >" + "使用者名稱不可為空!" + "</b>");
return false;
}
else if (!reg.exec(username)) {
$(‘#Username‘).after("<b id=‘msg1‘>" + "使用者名稱只能用中文、英文、數字、底線、4-16個字元!" + "</b>");
return false;
}
else {
return true;
}
}
function password() {
$(‘#msg1‘).remove();
var password = $("#Password").val();
if (password == null || password == "") {
$(‘#Password‘).after("<b id=‘msg1‘>" + "密碼不可為空!" + "</b>");
return false;
}
else {
return true;
}
}
function confirmPassword() {
$(‘#msg1‘).remove();
var password = $("#Password").val();
var comfirmpassword = $("#ConfrimPassword").val();
if (comfirmpassword == null || comfirmpassword == "") {
$(‘#ConfrimPassword‘).after("<b id=‘msg1‘>" + "確認密碼不可為空!" + "</b>");
return false;
}
else if (comfirmpassword != password) {
$(‘#ConfrimPassword‘).after("<b id=‘msg1‘>" + "兩次密碼輸入不一致!" + "</b>");
return false;
}
else {
return true;
}
}
function telephone() {
$(‘#msg1‘).remove();
var telephone = $("#Telephone").val();
var reg = new RegExp("^(13[0-9]{9})|(14[0-9])|(18[0-9])|(15[0-9][0-9]{8})$");
if (telephone == null || telephone == "") {
$(‘#Telephone‘).after("<b id=‘msg1‘>" + "電話號碼不可為空!" + "</b>");
return false;
}
else if (!reg.exec(telephone)) {
$(‘#Telephone‘).after("<b id=‘msg1‘>" + "電話號碼格式不正確!" + "</b>");
return false;
}
else {
return true;
}
}
function email() {
$(‘#msg1‘).remove();
var email = $("#Email").val();
var reg = new RegExp("^([a-zA-Z0-9_-])[email protected]([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$");
if (email == null || email == "") {
$(‘#Email‘).after("<b id=‘msg1‘>" + "郵箱不可為空!" + "</b>");
return false;
}
else if (!reg.exec(email)) {
$(‘#Email‘).after("<b id=‘msg1‘>" + "郵箱格式不正確!" + "</b>");
return false;
}
else {
return true;
}
}
var returnvalue = false;
function registeryanzheng() {
var username = $("#Username").val();
var quanxian = $("#DropDownListPermission").val();
var a = "使用者名稱或許可權存在";
$.ajax({
type: "POST",
url: "register.aspx?action=ajax",
data: "z_username=" + username + "&&z_permission=" + quanxian,
success: function (msg) {
$(‘#msg1‘).remove();
$("#Submit").attr("disabled", false);
if (msg.indexOf(a) > -1) {
$(‘#DropDownListPermission‘).after("<b id=‘msg1‘>" + msg + "</b>");
//判斷按鈕是否可用
$("#Submit").attr("disabled", true);
returnvalue = false;
}
else {
$("#Submit").attr("disabled", false);
returnvalue = true;
}
}
});
}
function yanzheng() {
if (!username()) {
return false;
}
if (!password()) {
return false;
}
if (!confirmPassword()) {
return false;
}
if (!telephone()) {
return false;
}
if (!email()) {
return false;
}
if (!registeryanzheng()) {
registeryanzheng();
return returnvalue;
}
else {
return true;
}
}
</script>
後台:
if (!String.IsNullOrEmpty(Request["action"]) && Request["action"] == "ajax")
{
string username = Request["z_username"];
string permission = Request["z_permission"];
SqlConnection conn = new SqlConnection(url);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
cmd.CommandText = "select * from zmt_user where z_username = ‘" + username + "‘"
+ " and z_permission = ‘" + permission + "‘";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Write("使用者名稱或許可權存在");
//System.Threading.Thread.Sleep(2000);
}
else {
Response.Write("不存在");
}
}
catch (Exception ex)
{
Console.WriteLine("error");
}
finally
{
Response.End();
conn.Close();
}
Response.End();
}
注意傳回值的接收
javascript、ajax驗證