2016-03-23 36 views
6

Continuo a ricevere strani errori di convalida inaspettati con il mio campo numerico HTML5. Questo è il mio codice HTML:Perché non posso inserire 500 in un campo numerico HTML5?

<input type="number" name="width" maxlength="5" placeholder="Width" value="733.95591182365" />&nbsp;Width<br /> 

Quando entro 500 e inviare il modulo, ricevo l'errore: "Si prega di inserire un valore valido I due valori validi più vicini sono 499,95591182365 e 500,95591182365."

Ho risolto il problema. Il problema era questo value="733.95591182365". Ho modificato il mio codice PHP per arrotondare i decimali a numeri interi in modo che solo i numeri interi possano essere riportati nel campo del numero. Ora ricevo solo errori quando si inseriscono i decimali, il che è OK.

+2

'maxlength' non si applica agli ingressi numerici. – Oriol

risposta

7

È necessario disporre di step="any" come un attributo quando si lavora con type="number"

<form> 
    <input step="any" type="number" name="width" maxlength="5" placeholder="Width" value="733.95591182365" /> 
</form> 
+0

Ho contrassegnato la risposta come corretta perché funzionerebbe anche, ma ho risolto il problema arrotondando la larghezza a un numero intero prima di farlo eco. –

+0

Questo passo = "qualsiasi" dovrebbe essere stato predefinito per type = "numero". Grazie! – Jack0fshad0ws

6

Per number inputs,

The step scale factor is 1. The default step is 1

Poi, dal momento che non v'è alcuna step attributo, il allowed value step è 1×1 = 1:

E il step base sarà 733.95591182365:

  1. If the element has a value content attribute, and the result of applying the algorithm to convert a string to a number to the value of the value content attribute is not an error, then return that result and abort these steps.

Pertanto,

Constraint validation: When the element has an allowed value step , and the result of applying the algorithm to convert a string to a number to the string given by the element's value is a number, and that number subtracted from the step base is not an integral multiple of the allowed value step , the element is suffering from a step mismatch .

Dal 733.95591182365 - 500 = 233.95591182365 non è un multiplo di 1, quel vincolo è violato.

Se si desidera un altro passaggio, specificare un attributo step. Se non si desidera alcun passaggio, utilizzare step = "any".