google chrome - Validate number type inputs with jQuery -


i having quite complex problem dealing jquery validation on optional fields have type="number" specified. happens browser (chrome in case) allows user enter normal letters in field, idea of intercepting submitting of form , anyway, when field value queried, return empty string.

before going forward, must kind-of in line specs, states:

user agents must not allow user set value non-empty string not valid floating-point number.

and later on:

the value sanitization algorithm follows: if value of element not valid floating-point number, set empty string instead.

here lies problem: if enter letters in optional number input fields (and chrome allows that), jquery validation gets empty string value validates correctly since field optional. moreover, emtpy string server side ok since field nullable in db , happily gets saved.

for time being, workaround can think of this:

$('input[type="number"]').keypress(function (evt) {     if (evt.keycode < 48 || evt.keycode > 57)         evt.preventdefault(); }); 

problem above not work, example, if user copy-pastes wrong input there. now, realize if user determined enter dumb data can face consequences , have field nullified, trying developer wondering if there way of obtaining value entered without using workarond above (and without writing workaround copypaste).

jquery.alphanum plugin wrote prevent invalid text being entered in first place. handles copy-paste , cross-browser compatible.

$('input[type="number"]').numeric(); 

the plugin traps paste event , filters text before becomes visible user.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -