userscripts - Obtain and check the value of an input text with javascript -
i new javascript , need script obtain value of input text , check value if equal 0 (zero). if need alert message notifying me text input 0. thanks.
<input id="txtval" type="text" name="txtval" value="0" maxlength="10">
the short answer:
if(txval.value==0) alert('please enter number.');
is not best answer:
var v=document.getelementbyid('txtval'); v=parseint(v,10); if(v<=0) alert('please enter positive integer.');
of course not best answer either fix couple of common mistakes.
- although can refer element using id variable name not recomended.
- the
value
property of input (ietxtval.value
) string (even if contains digits) if try compare number may unexpected results. in case comparing 0 problems less if ever change code compare number run problems, best make actual number before it.
if want allow decimals use parsefloat
instead.
Comments
Post a Comment