开发者

How do i only allow a text box to only have numbers and the following percent symbol?

I am currently building an application and I want a javascript or jquery condition that only allows numbers to be entered into the text box and/or the following percent but i开发者_开发知识库 am not sure how to obtain this... I found this but how do i allow the following percent


$(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/


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;
    });


Try this. 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;
  }
});


Hehe this is what I came up with

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

This should be used in the kepress jquery event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜