The code below is from an HTML form. If the input is supposed to be an integer, do I need to change the "type'?
<div class="friend2title">
<label for="url">Add points:</label>
</div>
<div class="friend2field">
<input name="state" type="text" id="state" maxlength="150">
</div>
8 Answers
<input type="number" step="1" ...
By adding the step attribute, you restrict input to integers.
Of course you should always validate on the server as well. Except under carefully controlled conditions, everything received from a client needs to be treated as suspect.
This might help:
<input type="number" step="1" pattern="\d+" />
step is for convenience (and could be set to another integer), but pattern does some actual enforcing.
Note that since pattern matches the whole expression, it wasn't necessary to express it as ^\d+$.
Even with this outwardly tight regular expression, Chrome and Firefox's implementations, interestingly allow for e here (presumably for scientific notation) as well as - for negative numbers, and Chrome also allows for . whereas Firefox is tighter in rejecting unless the . is followed by 0's only. (Firefox marks the field as red upon the input losing focus whereas Chrome doesn't let you input disallowed values in the first place.)
Since, as observed by others, one should always validate on the server (or on the client too, if using the value locally on the client or wishing to prevent the user from a roundtrip to the server).
If you're using HTML5, you should use the input type number. If you are using xhtml or html 4, input type should be text.
Prior to HTML5, input type="text" simply means a field to insert free text, regardless of what you want it be. that is the job of validations you would have to do in order to guarantee the user enters a valid number
If you're using HTML5, you can use the new input types, one of which is number that automatically validates the text input, and forces it to be a number
keep in mind though, that if you're building a server side app (php for example) you will still have to validate the input on that side (make sure it is really a number) since it's pretty easy to hack the html and change the input type, removing the browser validation
What about this:
<input type="number" step="1" onchange="this.value = parseInt(this.value);">
It just takes whatever you wrote, and tries to make an integer out of it (if it can't make an integer it will just leave it blank).
Also, let's say you're asking for numbers between 0 and 20:
<input type="number" step="1" onchange="this.value = Math.max(0, Math.min(20, parseInt(this.value)));">
This way you prevent people from writing numbers outside of the boundary (which is not prevented by your standard min= and max=).
You should change your type to number If you accept decimals first and remove them on keyUp, you might solve this...
$("#state").on('keyup', function(){
$(this).val($(this).val().replace(".", ''));
})
or
$("#state").on('keyup', function(){
$(this).val(parseInt($(this).val()));
})
This will remove the period, but there is no 'Integer Type'.
No, it is not about the data type of input. It specifies the type of control to create:
type = text|password|checkbox|radio|submit|reset|file|hidden|image|button [CI] This attribute specifies the type of control to create. The default value for this attribute is "text".
Even if you want to accept numbers for the input, I would recommend using the text type.
<input type="text" name"some-number" />
Client-side run some jQuery validations to verify it's a number.
Then in your server side code, run some validation to verify it is in fact a numerical value.