Jquery Mask without using plugin beginner [duplicate] - javascript

This question already has answers here:
Implement an input with a mask
(10 answers)
Closed 1 year ago.
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?

As far as I understood, your question is how to do the following:
Mask input field
Input field can only accept number
So I do the following steps:
Make input field as password type.
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>

Related

How to watch if HTML 5 form is ready to submit? [duplicate]

This question already has answers here:
Trigger standard HTML validation (form) without using submit button? [duplicate]
(13 answers)
Closed 1 year ago.
Using a simple form like that, how to watch if it can be submitted?
<form action="/action_page.php">
<label for="country_code">Country code:</label>
<input type="text" id="country_code" name="country_code"
pattern="[A-Za-z]{3}" title="Three letter country code"><br><br>
<input type="submit">
</form>
I mean, if some inputs are using pattern and / or required, how to know if the whole form is ok to be submitted?
I’ve found the oninvalid attribute for a specific input. It is possible to use an onvalid on the <form> element?
You can do something like this:
<form action="/action_page.php" onsubmit="submit()">
<label for="country_code">Country code:</label>
<input type="text" id="country_code" name="country_code" pattern="[A-Za-z]{3}" placeholder="Three letter country code"><br /><br />
<input type="submit" value="load">
</form>
<script type='text/javascript'>
fuction submit(){
//get the value of the input
var code = document.getElementById('country_code').value;
//use a regular expression
var expression = [A-Za-z]{3};
if(!expression.test(code)){
//when returning false the form will not be sent
return false;
}
else{
//when returning true the form will be sent
return true;
}
}
</script>
You can do this for all fields although there are more ways this can serve you

HTML allow only number without dot (.) in <input type="number"> [duplicate]

This question already has answers here:
HTML text input allow only numeric input
(78 answers)
Closed 2 years ago.
HTML
<input type="number" required="" pattern="\d*" id="mobile">
jQuery
var letters = /^[0-9]+$/;
var mobile_backup="";
$(document).ready(function(){
$("#mobile").on("input", function(){
var mobile_value=$(this).val();
if(mobile_value==="")
{
mobile_backup=mobile_value;
}
else if(mobile_value.match(letters)===null){
$(this).val(mobile_backup);
}
else{
mobile_backup=mobile_value;
}
});
});
Current solution works in all devices (desktop, mobile, tablet).
Is there any efficient approach available such that it works in all devices?
I know I can use a pattern for number. But I don't want an alert. I just want to allow the numbers without a dot.
Use pattern attribute to add regex expression in html tag.
Try below code:
<form action="home_page.php">
<label for="roll_no">Roll Number:</label>
<input type="number" id="roll" name="roll" pattern="^[\d]+$" title="Write your roll number"><br><br>
<input type="submit">
</form>
Note :
The pattern attribute of the input tag is not supported in Safari 10 (or earlier).
Validation of pattern attribute will not prevent from writing in the field but it will prevent from submitting form.
try step="any"
This allows a decimal point

Change what is inside an input type text field [duplicate]

This question already has answers here:
Set the value of an input field
(17 answers)
Set Value of Input Using Javascript Function
(7 answers)
Closed 2 years ago.
I have an input field as follows:
<input type="text" id="screen" maxlength="20">
When I type in it, I see text, but there is no change to the html reflecting this.
How do I change the text inside this input using jQuery?
I tried the following but it didn't work:
document.getElementById('screen').innerHTML = "10";
Here's an older example of how to change an input value using JQuery:
jQuery change input text value
$('#screen').val('10');
Else with vanilla Javascript, AlwaysHelping is right with
document.getElementById('screen').value = "10";
Set the "value" attribute of an <input> element.
document.getElementById("screen").value = 10;
// or if you select the element with chrome dev tool
$0.value = 10
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#htmlattrdefvalue
In order to do so, you can simply listen to the input cha
function updateInput(inputValue){
console.log("User Input",inputValue)
}
<input type="text" name="myInput" oninput="updateInput(this.value)" />
nge and print it again on HTML.
Use the val method:
$('#screen').val(10);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="screen" maxlength="20">

hiding text entered in a type text input [duplicate]

This question already has answers here:
remove value attribute of input element using CSS only
(4 answers)
Closed 5 years ago.
I have a card reader which connects to computer via USB,it's supposed to print a 10 digit number when an input is focused on and a card is placed over the card reader, it works fine but I need the code to be invisible. when I hide the text using color:transparent, the text is hidden but when the user double clicks on the input it shows the text!
Thanks in advance.
Use this
<input type="password" name="pin" value="abc123">
And if you want to hide it completely then use
<input type="hidden" name="pin_hidden" value="abc123">
Please see below-
.hide {
text-indent: -99em;
}
<input type="text" value="123456789"><br>
<input class="hide" type="text" value="123456789">

I am developing firefox extension and wanted to count total number of text box of webpage but I wanted to ignore hidden textboxes. How to do this? [duplicate]

This question already has an answer here:
I am developing firefox extension and wanted to count total number of text box of webpage so how to count text box? [closed]
(1 answer)
Closed 9 years ago.
I am developing firefox extension and wanted to count total number of text box of webpage but I wanted to ignore hidden textboxes. How to do this? Every webpages has some hidden text fields for future reference so how to ignore them so that i can count only those text boxes which i can see.
use input:visible to find all non hidden text elements
<!-- assuming you have included jquery-->
<div id="test">
<input type="text" hidden>
<input type="text">
<input type="text">
<input type="text">
<input type="text">
</div>
<script>
alert($('#test').find('input:visible').length);
</script>
http://jsfiddle.net/2ff2Y/

Categories

Resources