jquery - javascript add two multipleevents on input field -
i trying add 2 events on input field onkeyup , onchange, purpose avoid longpress of characters other numbers..as field zipcode. @ present 1 event working either keypress/onchange. adding code refrence appreciated.
function zipchange(obj, selector){ var code = obj.value; var isnum = /^\d+$/.test(code); if(!isnum) obj.value=""; }//onchange function autozip(obj, selector){ var code = obj.value; if(code.match(/\d/gi)) obj.value = code.replace(/\d/gi,''); if(code.length>4 && code.indexof('-')== -1){ var substr = code.substring(4); substr=substr.replace(/\d/gi,''); obj.value = code.substring(0,4)+'-'+substr; }//onkeypress //html <input id="pincode" type="text" data-validate="validate[required]" name="address.pincode" required="true" onkeyup="autozip(this)" onchange="zipchange(this)" msg="enter valid zip code" />
answer:
function autozip(obj, selector){ var code = obj.value; if(code.match(/\d/gi)) obj.value = code.replace(/\d/gi,''); if(code.length>4 && code.indexof('-')== -1){ var substr = code.substring(4); substr=substr.replace(/\d/gi,''); var substr1 = code.substring(0,4); obj.value = substr1+'-'+substr; var isnum = /^\d+$/.test(substr1) if(!isnum) obj.value=""; }
hi above modified function did trick..thanks enlightned ones helped me..
well first of onchange triggered when change content of text-box , when loose focus input type
hence there no use in using onchange event have implement onchange logic in keyup event
Comments
Post a Comment