Switch statement with Form in Javascript for Number [duplicate] - javascript

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.

Related

Html/javascript: Unable to disable Textbox

Not really sure how to phrase the question but I have created a function where if a button is disabled, the textbox below should also be disabled, but it's not. Here's the code:
<script>
function Play(){
if (document.getElementById("high").value > document.getElementById("low").value){
document.getElementById("myBtn").disabled = false;}
else {document.getElementById("myBtn").disabled = true;
document.getElementById("ErrorForPlay").innerHTML = "Numbers Wrong."}
}
</script> //this script checks to see if the higher number is less than the lower number. Will be disabled if so.
<br>
<button id="myBtn" onclick="Play()">Play</button>
<span id="ErrorForPlay"> </span>
<!--assume that the button on top is always disabled-->
GUESS: <!--But the textbox does not disable-->
<br>
<Input type="number" id="guess" onclick="dataValidationG()" onchange="dataValidationG()"> </Input>
<button id="submit">Enter Guess</button>
<script>
function dataValidationG(){
if (document.getElementById("myBtn").disabled = true;){
document.getElementById("guess").disabled = true;}
else {document.getElementById("guess").disabled = false;}
//script to check. If Button "myBtn" is disabled, the text should also disable if not, the text should be enabled.
}
</script>
I'm sure it has something to do with the javascript but I can't place where did I go wrong/what's missing. (I'm also only allowed to do everything under 1 html file.)
The problem is in your if statement conditions, you are using = and not ==.
And thanks to JavaScript being such a nice programming language, it doesn't throw a syntax error, but instead sets the value inside the if statement condition!
So just replace the = with == inside your if statement conditions.

Need to make the javascript function more refined [duplicate]

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') {}

Javascript NaN function not working [duplicate]

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).

Validating an Email in JavaScript [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 6 years ago.
I'm using the pattern:
var pattern = /^[^\s#]+#[^\s#]+\.[^\s#]+$/;
So when I submit/send an email to myself from my contact form to test out a bunch of different email combinations, everything has worked except for:
whatever#yahoo.com && whatever#google.com
I'm not entirely sure why those two aren't being included, but I'd appreciate any assistance.
I guess you miss some chars in your pattern,
you may find here an answer
Validate email address in JavaScript?
there is a code which handles more chars that you forgot, and the comment above too.
I'm not sure whats going wrong with yours, but here's an email pattern I've used successfully.
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$"
This help you :
var patt = /^[A-Za-z0-9]+#[A-Za-z0-9]+\.[A-Za-z]+$/gmi
final code :
<html>
<head>
</head>
<body>
Enter Your Email : <input type="tel" id="email">
<button onclick="isvalid()">Try</button>
<p id="res"></p>
<script>
var res = document.getElementById("res");
var patt = /^[A-Za-z0-9]+#[A-Za-z0-9]+\.[A-Za-z]+$/gmi
function isvalid(){
str = document.getElementById("email").value;
if(patt.test(str))
res.innerHTML = "Email is Valid";
else
res.innerHTML = "Email is InValid";
}
</script>
</body>
</html>

Unable to Limit the user input in JavaScript [duplicate]

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;
}
}

Categories

Resources