This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I am trying to validate a text box to enter only numbers and not alphabets using javascript NaN function.
But i am not getting the correct output
<html>
<body>
<input type="text" id="demo">
<button onclick="myFunction();">Try it</button>
<script>
function myFunction()
{
var x =document.getElementById("demo");
if (isNaN(x))
{
alert("hi");
}
else
{
alert("world");
}
}
</script>
</body>
</html>
x in your code will be an HTMLInput object. It will never be a number.
You want to test the value of parseInt(x.value).
Related
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 months ago.
<script>
var idn_text = "123"
document.getElementById("idn_id").value = idn_text
</script>
<p><input type="text" placeholder="Results" name="idn" id="idn_id"></p>
the webpage does not display this value, it only displays it as an empty form. How to fix it?
Your input field has not rendered and the script is looking for an element with it's id. A simple solution is to move your script to end of the html file. like this:
<p><input type="text" placeholder="Results" name="idn" id="idn_id"></p>
<script>
var idn_text = "123"
document.getElementById("idn_id").value = idn_text
</script>
Switch the order of your elements and call the script afterwards.
<p><input type="text" placeholder="Results" name="idn" id="idn_id"></p>
<script>
var idn_text = "123"
document.getElementById("idn_id").value = idn_text
</script>
This question already has answers here:
Allow only numbers to be typed in a textbox [duplicate]
(3 answers)
Closed 5 years ago.
As the title say I've written a small code piece to detect if the entered character is a number or a letter and below is the script code.
function checkNum(i) {
//language=JSRegexp
var txt = i.value;
if (isNaN(txt)){
document.getElementById("msg").innerHTML = "Numbers only";
return false
}else{
return true
}
}
<label for="volume">Volume:</label>
<input type="text" name="volume" id="volume" size="4" onkeyup="checkNum(this)" style="margin-left:23px;">
<label for="noPl" style="margin-left: 35px;">No. of Product Lines:</label>
<input type="text" name="noPl" id="noPl" size="4" onkeyup="checkNum(this)">
<div id="msg"></div>
I tried to refine this more but when I change this it stops working for some reason.
What I want this to do is not only prompt the message but also to clear out any entered character from the text box and only allow to enter number.
At the current state it's only prompting the user not to enter letters. I did try many other mentioned methods here but none of them were successful until this.
So if can please enlighten me on what to do also keep in mind I'm still learning JavaScripting not pro yet.
$(function() {
$('#staticParent').on('keydown', '#child', function(e){-1!==$.inArray(e.keyCode,[46,8,9,27,13,110,190])||/65|67|86|88/.test(e.keyCode)&&(!0===e.ctrlKey||!0===e.metaKey)||35<=e.keyCode&&40>=e.keyCode||(e.shiftKey||48>e.keyCode||57<e.keyCode)&&(96>e.keyCode||105<e.keyCode)&&e.preventDefault()});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="staticParent">
<input id="child" type="textarea" />
</div>
You can check
if( typeof txt == 'number' || typeof txt == 'string') {}
This question already has answers here:
Is "clear" a reserved word in Javascript?
(4 answers)
Closed 6 years ago.
function clear() {
document.getElementById("1").innerHTML = '';
document.getElementById("2").innerHTML = '';
document.getElementById("3").innerHTML = '';
}
<button onclick="clear()">Clear</button>
That is the function and my button that runs it.
The purpose of the function is to clear the paragraphs, but the button does nothing when I tested it out.
Why is this function not running when the button is clicked?
EDIT: sorry if the answer is obvious, i'm a beginner to JS
EDIT: HTML:
<p id="1"></p>
<p id="2"></p>
<p id="3"></p>
There is also a JS function that changes the text of the Paragraphs, but it doesn't loop
Don't name the function clear as the DOM has an old global method named clear. Try something else like clearMe
function clearMe() {
document.getElementById("1").innerHTML = '';
document.getElementById("2").innerHTML = '';
document.getElementById("3").innerHTML = '';
}
<div id="1">
foo
</div>
<div id="2">
foo
</div>
<div id="3">
foo
</div>
<button onclick="clearMe()">Clear</button>
This question already has answers here:
Switch statement for greater-than/less-than
(10 answers)
Closed 8 years ago.
Please can someone help me with a code doing a switch statement with a form to check
something like:
<script language="JavaScript">
<!--
function verifyPerf(form){
var myEntry = form.number(); //check the edit box number, didn't know how?!
var firstPart = "1. You need to do thing 1";
var endPart = "2. You need to to do thing 2";
switch(myEntry){
case "<= 3000" :
alert(firstPart);
break;
case ">3000 and <9000" :
alert(endPart);
break;
//would like to add 3 more cases
default :
alert('You have entered an invalid performance number');
}
}
-->
</script>
</head>
<body>
<form name="myForm">
<b>Please enter your performance number:</b><br>
<input type=number value="" name="perfNumber">
<input type=BUTTON value="Verify" name="myButton" onClick='verifyPerf(this.form)'>
</form>
</body>
</html>
thanks for your help.
Lx
There is alos this part that's not working.
function verifyPerf(form){
var myEntry = form.value;
.....
and the form:
Please enter the Performance:
Your switch statement is looking for the string '<= 3000' or '>3000 and <9000' not a number.
Check this out: Switch on ranges of integers in JavaScript
This demonstrates how to properly switch against a range of numbers.
This question already has answers here:
How to impose maxlength on textArea in HTML using JavaScript
(16 answers)
Closed 8 years ago.
I tried limiting the user input but it wasn't successful, please guide me where I am making mistake.
JS
<script type="text/javascript">
function countLength() {
var maxLength=10;
var length = document.getElementById("txt").value.length;
if(length>10) {
return false;
}
}
</script>
HTML code
<form name="formA" id="formA" action="#" >
<textarea id="txt" name="txt" onkeyup="countLength()"></textarea>
</form>
Your code basically replicates the maxlength attribute, which seems to work (and I don't think is being deprecated?). Just use that.
<input type='text' name='mytext' maxlength='10'>
return false on onkeyup does nothing (as you've probably noticed). I've seen solutions where someone would just alter the value of the textarea, perform a substring operation, and assign that new value back.
Try this:
function countLength() {
var maxLength=10;
var ta = document.getElementById("txt");
var length = ta.value.length;
if(length>maxLength) {
ta.value = ta.value.substr(0, maxLength);
return false;
}
}