jquery - Clear input box when input:invalid in html using javascript -
jquery - Clear input box when input:invalid in html using javascript -
my goal clear input type text box when focused if input invalid. let's have text box has maximum value of 200
<input id="textbox" type = "text" max="200" runat="server">
so let's input 150 it's valid hence if focus 1 time again on it, remain value 150 but, if input greater 200, text box have have reddish border color on , focused on textbox(its working on current code) , if focus on again, want clear value.
i wanna this.
input:invalid { box-shadow: 0 0 1px 3px #e74c3c; } input:focus:invalid { textbox.value = "" (i know not work, wanna how?) }
note have multiple textboxes different max , min values, not one.
you can utilize validitystate interface enables check state of each input field. first need hear onfocus
event. example:
var form = document.queryselector('form'), elements = form.queryselectorall('input'), i; (i = 0; < elements.length; i++) { elements[i].addeventlistener('focus', function() { if (!this.validity.valid) { this.value = ''; } }, false); }
demo: http://jsfiddle.net/yrovm6uh/ javascript jquery html css textbox
Comments
Post a Comment