This question already has answers here:
HTML text input allow only numeric input
(78 answers)
Closed 8 years ago.
How can I make to accept only numbers inputs in my code: -->> Here ?
Thank you !
You can do this:
<input type="number">
or this:
<input type="text" pattern="[0-9]+">
These will only work in HTML5 compatible browsers.
In javascript you can do this:
JSFiddle
HTML:
<input type="text" id="test" name="test"/>
CSS:
var input = document.getElementById("test");
input.oninput = function(e){
if (/\D/g.test(this.value))
{
// Filter non-digits from input value.
this.value = this.value.replace(/\D/g, '');
}
}
Related
This question already has answers here:
How to use toLocaleString() and tofixed(2) in JavaScript
(3 answers)
Closed 2 years ago.
I have an input where the user can enter a number. However the number displayed should be set to 2 decimal places while considering the culture.
For example, if the user enter the number 85,
"85,00"
should be displayed if the culture is french and
"85.00"
should be displayed if the culture is English.
I have the below code for it:
$(document).on("change", ".changeVal", function (e) {
var valueEntered = parseFloat($(this).val());
valueEntered = valueEntered.toFixed(2);
valueEntered = (valueEntered).toLocaleString("fr-FR")
$(this).val(valueEntered);
});
However, this is not setting the input to "85,00".
But when I do,
console.log((80.22).toLocaleString("fr-FR"));
the displayed value is "80,22";
Any idea of what I might be missing here?
HTML:
<input class="mdl-textfield__input text-right placeholder validate-number changeValue" maxlength="150" type="text">
Just use parseFloat after toFixed because it convert the number to a string.
`$(document).on("change", ".changeVal", function (e) {
var valueEntered = $(this).val();
valueEntered = parseFloat(valueEntered.toFixed(2));
valueEntered = (valueEntered).toLocaleString("fr-FR")
$(this).val(valueEntered);
});`
This question already has answers here:
How do I select elements on multiple attribute values
(6 answers)
Closed 3 years ago.
Assuming you have a large number of inputs of type 'file', like so:
<input type="file" id="fileSomething1" />
<input type="file" id="fileSomething2" />
Is it possible to select all inputs of type 'file' that the user has loaded at least 1 file into?
I tried this, but it didn't work:
$("input[type='file' && value!='']").length
You have incorrect syntax for chaining of multiple attribute equals selector. It should be:
$("input[type='file'][value!='']").length
Working Demo
I would also suggest you to consider using .filter() selector for comparing value condition.
This code enable loop checking for input type file,If any of it find having value then it would print
$("#butn").on("click", function() {
$("input[type=file").each(function() {
if ($(this).val() != "") {
console.log($(this).attr('id'))
}
});
})
$("input[type=file").on("change", function() {
if ($(this).val() != "") {
console.log($(this).attr('id'))
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="fileSomething1" />
<input type="file" id="fileSomething2" />
<button id="butn">click</button>
This question already has answers here:
Transform numbers to words in lakh / crore system
(34 answers)
Closed 4 years ago.
How to accepts an array of numbers and returns an array of numbers in written form e.g.
[0,0,6,2,7] → [“zero”, “zero”, “six”,"two","seven"]
I want to input and return array of values not one value?
<div>
<input type="text" [(ngModel)]="number" placeholder="Input Number"/>
<div id="word">{{words[number]}}</div>
</div>
{{numbers[number]}}
<input type="text" [(ngModel)]="stringOfNumbers" placeholder="Input Number"/>
{{stringOfNumbers}}
<br>
{{arrayOfNumbers}}
words= ['zero','One','Two','Three','Four','Five','Six','seven','eight'];
stringOfNumbers = "1,2,3,4";
arrayOfNumbers = this.stringOfNumbers .split(',');
Improve my comment: (the stackblitz here)
//The .html
<input [ngModel]="numbers" (ngModelChange)="calculeNumber($event)">
{{result|json}}
//The .ts
export class AppComponent {
numbers:string;
result:string[]=[];
words= ['zero','One','Two','Three','Four','Five','Six','seven','eight'];
calculeNumber( numbers:any){
this.result=numbers.split(',').map(x=>
{
return this.words[+x]
});
}
}
I suggest you use a middleware for this (check out this answer: Convert digits into words with JavaScript, it's pretty much hardcoding. Add an event listener every time the input value changes and loop through the array.
document.getElementsByTagName("input")[0].addEventListener('change', function(){
alert("Input changed!");
// Code goes here
});
This question already has answers here:
copy and concatenate values of multiple input boxes in a form to one input field
(2 answers)
Closed 5 years ago.
I want to concatenate the textInputs on clicking the button.
e.g text1=2 , text2=3 , text3=9 and text4=8, the final result should be 2398.
How to achieve this?
If you were using ES6 standards, you can use string literals
const txt1 = 'rt';
const txt2 = 'rete';
const concatenated = `${txt1}${txt2}`
you can refer the link for further learning:
https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/template_strings
create one onChangeHandler() for textInput such as:
<TextInput onChange={(text)=>(this.onChangeHandler("text1",text))}>
<TextInput onChange={(text)=>(this.onChangeHandler("text2",text))}>
<TextInput onChange={(text)=>(this.onChangeHandler("text3",text))}>
onChangeHandler=(name,value)=>{
this.setState({[name]:value})
}
then on button click do this:
onButtonClick=()=>{
let finalText =this.state.text1+this.state.text2+this.state.text3
console.log(finalText) //prints the concatenated text.
}
With Pure JS
function concatenate(){
var concatenate = document.getElementById("input1").value + document.getElementById("input2").value + document.getElementById("input3").value
document.getElementById("result").innerHTML = "Resultat:"+concatenate
}
<input id="input1" type="text" name="name1" >
<input id="input2" type="text" name="name2" >
<input id="input3" type="text" name="name3" >
<button type="button" onclick="concatenate()">concatenate</button>
<div id="result">Resultat: </div>
This question already has answers here:
jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points)
(40 answers)
Closed 9 years ago.
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.
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
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;
}
});