How to allow only a text field to have only numbers and the next percent symbol?

I am currently creating an application and I want a javascript or jquery condition that allows me to enter numbers in the text box and / or the following percentages, but I'm not sure how to get it ... I found, but how can I resolve the following percentages

+4
source share
4 answers
$(document).ready(function() { $("#my_input").keypress(function(event) { // Allow only backspace, delete, and percent sign if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 37) { // let it happen, don't do anything } else { // Ensure that it is a number and stop the keypress if (event.keyCode < 48 || event.keyCode > 57) { event.preventDefault(); } } }); }); 

Demo: http://jsfiddle.net/AlienWebguy/ufqse/

+4
source

try this one

  $(".common").submit(function(){ var inputVal = $('#input').val(); var characterReg = /^[0-9]+%?$/; if (!characterReg.test(inputVal)) { alert('in-correct'); } else { alert('correct'); } return false; }); 
+2
source

Try it. In this you can add what else you want to allow or restrict.

 $("textboxSelector").keypress(function(e) { var key = e.which; if ((key < 48 || key > 57) && !(key == 8 || key == 9 || key == 13 || key == 37 || key == 39 || key == 46) ){ return false; } }); 
0
source

Hehe this is what i came up with

  if(!parseInt(String.fromCharCode(event.keyCode))) event.preventDefault(); 

This should be used in the jQuery kepress event.

0
source

All Articles