[作成] ボタンをクリックすると、フィールドに値が入力されていなくてもページが更新されるだけなので、検証が機能しない理由がわかりません。しかし、ユーザー名フィールドに値を入力すると、REGularValidator も表示されます。
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="createmember.aspx.cs" Inherits="member" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script type="text/javascript">
$(document).ready(function () {
$("#MainContent_username").change(function () {
var uname = $("#MainContent_username");
var msgbox = $("#status");
if (uname.val().length < 6) {
msgbox.html('<font color="#cc0000">User Name must be more than 5 characters</font>');
} else {
$.ajax({
type: "POST",
url: "createmember.aspx/DoesUserExist",
data: "{'username': '" + uname.val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d == false) {
msgbox.html('<font color="Green">User Name Available </font>');
}
else {
msgbox.html('<font color="Red">User Name Exist</font>');
}
}
});
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<h2>
Create Member</h2>
<fieldset>
<div id="createform">
<div class="formlabel">
Login Name :</div>
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="username" ErrorMessage="RegularExpressionValidator"
SetFocusOnError="True"></asp:RegularExpressionValidator>
<span id="usernameempty"></span>
<span id="status"></span>
<br />
<div class="formlabel">
Password :</div>
<asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox>
<br />
<div class="formlabel">
First Name :</div>
<asp:TextBox ID="firstname" runat="server"></asp:TextBox>
<br />
<div class="formlabel">
Last Name :</div>
<asp:TextBox ID="lastname" runat="server"></asp:TextBox>
<br />
<div class="formlabel">
Email :</div>
<asp:TextBox ID="email" runat="server"></asp:TextBox>
<br />
<div class="formlabel">
User Role :</div>
<asp:DropDownList ID="role" runat="server">
<asp:ListItem Value="2">Admin</asp:ListItem>
<asp:ListItem Value="3">Staff</asp:ListItem>
<asp:ListItem Value="4">Driver</asp:ListItem>
<asp:ListItem Value="5">Manager</asp:ListItem>
<asp:ListItem Value="1">Super Admin</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="submit" runat="server" Text="Create" OnClick="submit_Click" />
<br />
<asp:Label ID="msg" runat="server" Text=""></asp:Label>
<div style="clear: both;" />
</div>
</fieldset>
</asp:Content>
私のバックエンドコード。
static string connectionString;
protected void Page_Load(object sender, EventArgs e)
{
connectionString = "SERVER=localhost;DATABASE=db;UID=root;PASSWORD=password;";
}
protected void submit_Click(object sender, EventArgs e)
{
string usertypevalue = role.SelectedValue.ToString();
string firstnamevalue = firstname.Text;
string lastnamevalue = lastname.Text;
string usernamevalue = username.Text;
string passwordvalue = password.Text;
if (!DoesUserExist(usernamevalue))
{
MySqlConnection connect = new MySqlConnection(connectionString);
MySqlCommand command = connect.CreateCommand();
command.CommandText = "Insert into user (UserTypeID, FirstName, LastName, LoginName, LoginPassword, DateTimeCreated, LastLoginDate) values ('"
+ usertypevalue + "','" + firstnamevalue + "','" + lastnamevalue + "','" + usernamevalue + "','" + passwordvalue + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','0000-00-00 00:00:00')";
try
{
//connect.Open();
// command.Connection = connect;
//command.ExecuteNonQuery();
msg.Text = "Member account created successfully";
msg.ForeColor = System.Drawing.Color.Green;
}
catch
{
msg.Text = "Error Occured, Please try again!";
msg.ForeColor = System.Drawing.Color.Red;
}
finally
{
connect.Close();
}
}
else
{
}
}
[System.Web.Services.WebMethod]
public static bool DoesUserExist(string username)
{
bool exist = false;
MySqlConnection connect = new MySqlConnection(connectionString);
try
{
connect.Open();
string query = "SELECT Count(*) FROM User WHERE LoginName = '" + username + "'";
//Create Mysql Command
MySqlCommand cmd = new MySqlCommand(query, connect);
//ExecuteScalar will return one value
int Count = int.Parse(cmd.ExecuteScalar() + "");
if (Count > 0)
{
exist = true;
}
}
finally
{
connect.Close();
}
return exist;
}