I have four textboxes and I need to check for null values. If any of the textbox value has null values then on button click I need to show a alert like 'Please enter values'.
This code will disable if textbox value is empty.
ASPX:
<tr style="height: 40px;">
<td>
<asp:textbox id="txt1" runat="server">
</asp:textbox>
</td>
<td>
<asp:textbox id="txt2" runat="server">
</asp:textbox>
</td>
<td>
<asp:textbox id="txt3" runat="server">
</asp:textbox>
</td>
</tr>
<tr>
<td>
<asp:button id="Button1" runat="server" text="Add" cssclass="button" width="50px"
onclick="Button1_Click" /></td>
</tr>
CS:
protected void Button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrWhiteSpace(txt1.Text) || String.IsNullOrWhiteSpace(txt2.Text) || String.IsNullOrWhiteSpace(txt3.Text))
{
string message = "Textbox can be empty, please enter a value";
string script = String.Format("alert('{0}');", message);
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msgbox", script, true);
}
if (txt1.Text != "" && txt2.Text != "" && txt3.Text != "" && ddlcategory.Text != "--Select--")
{
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(str))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
string strquery = "select * from product where code='Metal' and Id='" + txt1.Text + "'";
SqlCommand cmd = new SqlCommand(strquery, connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
if (ds.Tables[0].Rows.Count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(),"Key", "<script type='text/javascript'>window.onload = function(){alert('Product Already Exists.');return false;}</script>");
}
else
{
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("Products", conn);
cmd.Parameters.Add("@id", SqlDbType.VarChar, 30).Value = txt1.Text;
cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = txt2.Text;
cmd.Parameters.Add("@email", SqlDbType.VarChar, 30).Value = txt3.Text;
cmd.Parameters.Add("@category", SqlDbType.VarChar, 20).Value = ddlcategory.Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
ClientScript.RegisterStartupScript(this.GetType(), "Key", "<script type='text/javascript'>window.onload = function(){alert('Product Created Successfully.');return false;}</script>");
}
}
}
Anyone suggest me on this.
try below
if (String.IsNullOrWhiteSpace(txt1.Text) || String.IsNullOrWhiteSpace(txt2.Text) || String.IsNullOrWhiteSpace(txt3.Text) || String.IsNullOrWhiteSpace(txt4.Text))
{
string message = "Please enter values";
string script = String.Format("alert('{0}');", message);
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msgbox", script, true);
return;
}
You can do this in client side with JavaScript code
function verifyValues()
{
var txtBox1,txtBox2,txtBox3,txtBox4;
txtBox1=document.getElementByID("<%= myTextBox1.ClientID %>"):
txtBox2=document.getElementByID("<%= myTextBox2.ClientID %>"):
txtBox3=document.getElementByID("<%= myTextBox3.ClientID %>"):
txtBox4=document.getElementByID("<%= myTextBox4.ClientID %>"):
if(txtBox1.value=="" || txtBox2.value==""||txtBox3.value==""
|| txtBox4.value=="")
{
alert("Please enter any one of the value");
return false;
}
return true;
}
Then your server side Markup should call javascript function with return
statement
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return verifyValues();"
Text="Click" />
If you want double check both client and server side, please include @Damith answer in server side validation
I would use string.IsNullOrEmpty() instead and do the below.
if (string.IsNullOrEmpty(txt1.Text)) {
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"err_msg",
"alert('Please enter values!)');",
true);
}
try this instead of your code, and should be add return false
after alert message showed
if (string.IsNullOrEmpty(txt1.Text) ||string.IsNullOrEmpty( txt2.Text) || string.IsNullOrEmpty(txt3.Text) || string.IsNullOrEmpty(txt4.Text))
{
string script = "<script type=\"text/javascript\">alert('Please enter values');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
Button1.Enabled = false;
return false;
}
else
{
Button1.Enabled = true;
}
}
or
if (string.IsNullOrEmpty(txt1.Text) ||string.IsNullOrEmpty( txt2.Text) || string.IsNullOrEmpty(txt3.Text) || string.IsNullOrEmpty(txt4.Text))
{
string script = "alert('Please enter values');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
Button1.Enabled = false;
return false;
}
else
{
Button1.Enabled = true;
}
}
Try This one
if (txt1.Text == "" || txt2.Text == "" || txt3.Text == "" || txt4.Text == "")
{
ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "scrip1", "alert('Enter All fields');", true);
Button1.Enabled = false;
}
else
{
Button1.Enabled = true;
}
在所有文本框上应用必填字段验证,并为所有文本框提供组名称,对于单击哪个验证应该有效的按钮,所有文本框和按钮的组名称应相同。
This is exactly what you are looking for: (Server side validation for multiple control)
http://www.codeproject.com/Tips/722385/Single-Server-Side-Validator-for-Multiple-Controls
you have to do this validation at client side which gives u improved performance
as follow
function verifyValues()
{
var txtBox1,txtBox2,txtBox3,txtBox4;
txtBox1=document.getElementByID("txt2"):
txtBox2=document.getElementByID("txt2"):
txtBox3=document.getElementByID("txt2"):
txtBox4=document.getElementByID("txt2"):
if(txtBox1.value=="" || txtBox2.value==""||txtBox3.value==""
|| txtBox4.value=="")
{
alert("Please enter any one of the value");
return false;
}
return true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.