0
 var ratioChange = prompt('Are you sure to change seller ration of this user?');
                if(ratioChange != "")
                {
                  $('#clsdywusers_hdnaction').val("SET_SELLER_RATIO");
                  $('#clsdywusers_seller_ratio').val(ratioChange);
                }
                else
                {
                  alert('Please enter seller ratio.');
                  return false;
                }

Now here what I want is that I only want to allow users to write digits in prompt box.Please help.

  • You cant. The best you can do is create your own popup with validation, or test the returned value and repeat the prompt if its not valid –  May 26 '14 at 11:04
  • This has been asked before. Please see here: http://stackoverflow.com/questions/12539237/javascript-prompt-textbox-only-numbers – Waffles May 26 '14 at 11:04

1 Answers1

0

Use javascript input keypress event and check for every typed character if it's a number or not:

    function is_numeric(val){
        if(val > 47 && val < 58) return true;
        else return false;
    }

    $(".your_input").keypress(function(e){

            switch(e.which){
                    // exclude left and right navigation arrows from check
                case 0: case 8:break;
                default:
                    if(is_numeric(parseInt(e.which))) return true;
                    else{
                        return false;
                    }
            }
    });

Update: with prompt

    var ratioChange = prompt('Are you sure to change seller ration of this user?');
    if(ratioChange != "" && is_number(ratioChange))
    {
       $('#clsdywusers_hdnaction').val("SET_SELLER_RATIO");
       $('#clsdywusers_seller_ratio').val(ratioChange);
    }
    else
    {   
        alert('Please enter seller ratio.');
        return false;
    }

    function is_numeric(val){
        if(val > 47 && val < 58) return true;
        else return false;
    }

    function is_number(val){
        var value= new String(val), singleNumber;
        for(var i=0; i < value.length; i++){
            singleNumber = 48 + parseInt(value[i]);
            if(!is_numeric(singleNumber)) return false;
        }
        return true;
    }

JSBIN

Fares M.
  • 1,538
  • 1
  • 17
  • 18
  • 1
    In this case, you can't have full control on your `prompt` dialog, for this you can use a modal dialog, but if you really want it with `prompt` the only control you can do is to check the final value given by the client. – Fares M. May 26 '14 at 12:01