-4

Need to restrict entering alphabets and special characters in a textbox using jquery. Need to enter only numbers. How to implement using keypress() functionality in jquery for a text box in jsf?.

Code :

      <table>
         <tr>
          <h:inputlabel value="Actual"></h:inputlabel>
           <td>
              <h:inputtext id="Actual" styleClass="input-tex" value="#bean.customer"></h:inputtext>
            <td>
         </tr>
       <table>

Any help is appreciated.

Igor
  • 33,276
  • 14
  • 79
  • 112
Ramya
  • 1,067
  • 6
  • 16
  • 29
  • You can parse it into int and check – btevfik Mar 20 '13 at 17:02
  • Can you post the code of your [**current implementation**](http://whathaveyoutried.com) you are having issues with so we can have a look at it please? – Nope Mar 20 '13 at 17:02
  • 1
    have you tried to search for the answer first ? http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all – radu florescu Mar 20 '13 at 17:02

2 Answers2

0

I'm sure there's a more elegant way, but I use:

    $('#parentobject').on('keypress', '#inputid', function (e) {
        if (isNaN(e.currentTarget.value) == true) {
            var textlen = $(this).val().length;

            $(this).val($(this).val().substring(0, (textlen - 1)));

            return false;
        }
    });
whoopes
  • 134
  • 5
0

you can try this in onkeypress function

function validateDigits(){
    var str = "54665";
var re = /^\d+$/;
alert(str.match(re)!=null);
}

it returns true in case it validate successfully otherwise returns false

Mohamed Habib
  • 883
  • 1
  • 8
  • 18