c# - How to require text in a Textbox? -
how go requiring text in textbox? have far.
string strname = txtname.text; string stremail = txtemail.text; boolean blnerrors = false; if (strname == null) { } else { string script = "alert(\"name field required!\");"; scriptmanager.registerstartupscript(this, gettype(), "servercontrolscript", script, true); txtname.focus(); }
when run program , try execute it, error popping regardless if have text entered textbox or not. want error show if there nothing in textbox. have tried using,
if (strname == "")
as well. nothing changes.
change code :
string strname = txtname.text.trim(); //add trim here string stremail = txtemail.text; boolean blnerrors = false; if (string.isnullorwhitespace(sstrname)) //this function checks both null or empty string. { string script = "alert(\"name field required!\");"; scriptmanager.registerstartupscript(this, gettype(), "servercontrolscript", script, true); txtname.focus(); return;//return function there error. } //continue usual .
Comments
Post a Comment