<html> <head> </head> <body> <input id="i" value="mask"> <button id="b">delete</button> <script> b.onclick=function(){ if(i.value.length<5) return i.value=i.value.replace(/.$/, "") } </script> </body> </html>