-2

I would like to make an input type mask __/__ only numbers can be inputted without using any plugin just pure jquery any tips?

Another Question: For example user would input 3,, the output display will be 3/9 the (/9) will just be there and not editable cause 9 would be the maximum number of pages is this possible to achieve?

3
  • 1
    Whats your attempt so far? Commented Jul 24, 2018 at 6:40
  • none,,i'm new to this stuff and i only know this is easy if usage of plugin, but with no plugin is it possible?? Commented Jul 24, 2018 at 7:00
  • You really need to attempt a solution first before asking others to write one for you. Commented Jul 25, 2018 at 0:00

1 Answer 1

0

As far as I understood, your question is how to do the following:

  1. Mask input field

  2. Input field can only accept number

So I do the following steps:

  1. Make input field as password type.

  2. Detect the key in character while typing(keypress is done before value change), then don't change the existing value when the character is not numeric.

I added a button that you can click it to show current value.

$('#in').keypress(function(event){
  var reg = /[0-9]/g;
  var key = String.fromCharCode(event.keyCode);
  if(!reg.test(key))
    return false;
})

$('#show').click(function(event){
  alert($('#in').val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="in" type="password"/>

<br />

<button id="show">Show Value</button>

Update

Below is an example done by this plugin.

This may meet your requirement.

$('#inputtext').securenumbermask({mask:'_',maxlength:15});

$('#show').click(function(event){
  alert($('#inputtext-original').val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.jqueryscript.net/demo/jQuery-Plugin-To-Mask-Numbers-In-Text-Field-Secure-Number-Mask/js/securenumbermask.js">
</script>

<input class="form-control" type="text" id="inputtext" value="123" input-original="inputtext-original">

<input id="inputtext-original" type="hidden" value="**">

<br />

<button id="show">Show Value</button>

Sign up to request clarification or add additional context in comments.

4 Comments

Your english is fine, no need to apologise. I'm not sure using a password field is best. It might be a cause for suspicious behaviour. You could an input with <input type="number"> with the same effect
AFAIK <input type="number"> cannot mask input. I think <input type="type"> is able to do so but I'm not able to finish it because lots of conditions may be concerned. Sorry...
I've found a solution here: jqueryscript.net/form/…
Nice! Do you wanna update your answer with the plugin.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.