c# - How to i get my textbox to only allow numbers 1-14? -
this have far, getting error.
try { dblnights = convert.todouble(txtnights.text); if (dblnights > 1 && 14) { } else { string script = "alert(\"number of nights must between 1 , 14!\");"; scriptmanager.registerstartupscript(this, gettype(), "servercontrolscript", script, true); txtnights.focus(); } }//end try catch { string script = "alert(\"number of nights must integer!\");"; scriptmanager.registerstartupscript(this, gettype(), "servercontrolscript", script, true); txtnights.focus(); }//end catch
i not quite sure make show error box if numbers besides 1-14 entered. else working, not that. doing wrong?
thank you.
problem : not using logical and
operator.
this:
if (dblnights > 1 && 14) { }
must be:
if (dblnights >= 1 && dblnights <= 14) { /*valid range thing here*/ }
edit: suggested eric lippert in comments show usage of tryparse
.
if use double.tryparse()
can eliminate exceptions
may occur invalid data.because double.tryparse()
method return boolean
value true
if conversion successful otherwise returns false
avoid try catch
blocks.
try this:
double dblnights; if (double.tryparse(txtnights.text, out dblnights)) { //conversion successfull } else { //conversion failed }
Comments
Post a Comment