Textbox, Integer?

liunx

Guest
Just wondering, is there some way to confine the user to only typing in numbers in a textbox, and to go a bit further, if I can, to make sure the number is no less than 0, and is not a fraction or decimal (an integer, I believe you call them)?<br />
<br />
<form><br />
<input type="text" value="0" size=1 maxlength=3 /><br />
</form><br />
<br />
yeah, would that use the onBlur event, or could you stop them before they event type it?<br />
<br />
if this is javascript, then go ahead and move it, I just figured it's html since it's inside of tags.<!--content-->You cant stop them before they type it, but yuo can use java script (or preferably a server side script) to give them an error message when the submit.<!--content-->(or preferably a server side script)<br />
err, what? what's a 'server side script'?<!--content-->ASP PHP CGI ASP.NET JSP CF etc. They are scripts executed by the server instead of by the browser.<!--content-->Just a thought, If I were to fill that form and I took some time fill it in and then submit it only to find out that data was wrongly entered,I seriously won't bother again unless I have to , have to do that.<br />
<br />
A better option if available, in this particular situation will be to use JavaScript and warn user then and their before they spent 5 minutes in completing the form.<!--content--><form action=""><br />
<p><input type="text" name="foo" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"></p><br />
</form>But remember, users can disable JavaScript.<!--content-->That is why it is important to have server side scripts just incase, so users can't just turn off java script and send it anyway, say run it on button click.<!--content-->most of the page is javascript, and it's not a form where they submit stuff, it's just a form that modifies how the page appears. if they don't have javascript, there's no point in viewing the page. pyro, I'll try that.<br />
<br />
Edit: I tried it, and, it works, but if they use the keyboard loop, it doesn't catch it. press and hold "a".<br />
<br />
<form><br />
<input type="text" name="z" value=1 size=1 maxlength=3 onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }" /><br />
</form><!--content-->
 
Back
Top