Validation of percentage in javascript [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i can't seems to validate the percentage.
Need you guys help me to solve!
function validate()
{
s
if( document.myForm.outputPercentage.value <= 0||>=100
{
alert( "Please the correct percentage." );
document.myForm.outputPercentage.focus() ;
return false;
<form name="myForm" onsubmit="return(validate());">
Last output percentage: <input type"text" name="outputPercentage" id="outputPercentage" />&<br/><br/>
<input type="submit" value="Submit" />

I've corrected your code so that it works:
<html>
<head>
<script type="text/javascript">
function validate() {
var outputPercentageString = document.myForm.outputPercentage.value;
var outputPercentage = parseInt(outputPercentageString);
if (outputPercentage <= 0 || outputPercentage >= 100) {
alert("Please the correct percentage.");
document.myForm.outputPercentage.focus();
return false;
}
}
</script>
</head>
<body>
<form name="myForm" onsubmit="return(validate());">
Last output percentage: <input type="text" name="outputPercentage" id="outputPercentage" />%
<br/><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Remove the 's' all on its own.
Everything on a web page is a String, so to compare the percentage to a number you have to use parseInt to make it into a number.
type"text" should be type="text"
if you open a bracket { then you must close it afterwards }
Put the javascript into <script> tags in the <head> element of the page
I've put everything into <html> elements.
Learn what getElementById() does. Everybody uses ids to get references to webpage elements rather than names.
Hope this helps.

Related

How to update value of an attribute of an element, then trigger click on it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I would like to use jQuery to update value of attribute "accept" of an input element with type = "file", then active the "click" event of it. But after the value of "accept" is changed, nothing happens.
This is my code
//html elements
<button type="button" class="abc" title="Select files from your computer" id="upload" onclick="updateExtension()"></button>
<input type="file" name="image" id="image" multiple="multiple" /> //this input field is hidden and is applied with jquery-file-upload
//jQuery function
function updateExtension(){
$('#image').attr('accept', '.jpg, .png');
$('#image').click();
}
You can simply use :
$( "#image" ).trigger( "click" );
For more information on jQuery trigger click here.
Simple way use this one:--
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#click").click(function(){
//if update(div)content
$(".update_new_value").html("<h1>Enter content</h1>");
//if change attr value
$("#urls_change").attr("href", "https://www.changers.com/");
});
});
</script>
</head>
<body>
<div class="update_new_value"></div>
<p>change urls</p>
<button name="btn" id="click" type="button">Click Me</button>
</body>
</html>

Javascript in HTML document is not executed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
This is a copy of an HTML document from a tutorial on Javascript. Upon opening it in Chrome, it doesn't behave as intended. When I click the button with Set Cookie written on it, nothing shows up, regardless of whether I have written something in the box (we should get output on the page) or not (we should get an alert). Why?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie()
{
if( document.myform.customer.value == ""){
alert("Enter a name");
return;
}
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie="name=" + cookievalue;
document.write("Setting Cookies : " + document.cookie);
}
//-->
</script>
</head>
<body>
<form name="myform" action ="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onlick="WriteCookie();"/>
</form>
</body>
</html>
You have a typo in your input box. It should read:
<input type="button" value="Set Cookie" onClick="WriteCookie();"/>

link button using html5 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to create a button which allows me to go to a link entered in an input filed.
So this is what I did but it does not work.
<html>
<head>
</head>
<body>
<form action="link">
link : <input type="text" id="link">
<input type="button" value="go" onclick="self.location.href = 'docuement.getElementById('link').value'"/>
</form>
</body>
</html>
What should I do?
Don't use a string literal, get rid of the ' from around the value you assign to self.location.href.
Spell document correctly
You should avoid using self as well, it is an unnecessary level of indirection.
<input type="button" value="go" onclick="location.href = document.getElementById('link').value"/>
Things fixed:
fix spelling of document
unquote the expression to be evaluated
don't bother with self

OnType Javascript but want the type to match a word [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am working on an input box for a promo code. We have several promo codes. However, on one promo code, if the user types in "springchicken", then I need it to open, or toggle an additional radio input for a selection. Is that possible? I am thinking I could use onType, but I don't really know how to write that. See example below. I need it to so something like that but the words that are typed must match "SpringChicken". How can I write that?
<input type="text" name="promo" id="promo" value="" size="22" placeholder="Enter Promo Code" ontype="document.getElementsByClassName('promoradio')[0].style.display='block';" />
<!--Need the following DIV to appear once "SpringChicken" is entered on the fly-->
<div class="promoradio">
<input type="radio" name="promopkg" value="1" /><label>Promo Package #1</label>
<input type="radio" name="promopkg" value="2" /><label>Promo Package #2</label>
</div>
Try this jquery solution: (Edited so it won't be case sensitive)
<script type="text/javascript">
$(document).ready(function () {
$("#promo").keyup(function() {
var value = $(this).val().toLowerCase();
if (value == "springchicken") {
$(".promoradio").show();
} else {
$(".promoradio").hide();
}
});
});
</script>
There are a couple things wrong here.
First, there is no ontype event for javascript. You'll want onkeypress (for when they key goes down) or onkeyup for when the key goes up.
Second, inline javascript is ugly and bad practice. Use something like this:
var x = document.getElementById("input element");
x.onkeyup = function() {
var word = x.innerHTML;
if (word.toUpperCase() === "SPRINGCHICKEN") { //if you're not worried about case sensitivity or unicode
document.getElementsByClassName('promoradio')[0].style.display='block';
}
}
Assuming the class promoradio has display:hidden here initially.
Try this,
<input type="text" name="promo" id="promo" value="" size="22" placeholder="Enter Promo Code" />
<!--Need the following DIV to appear once "SpringChicken" is entered on the fly-->
<div class="promoradio">
<input type="radio" name="promopkg" value="1" /><label>Promo Package #1</label>
<input type="radio" name="promopkg" value="2" /><label>Promo Package #2</label>
</div>
<script>
var textToMatch = "springchicken";
function matchWord(e){
if(document.getElementById('promo').value.trim().toLowerCase() == textToMatch){
document.getElementsByClassName('promoradio')[0].style.display='block';
}else{
document.getElementsByClassName('promoradio')[0].style.display='none';
}
}
document.getElementById('promo').onkeyup = matchWord;
</script>
css:
.promoradio{
display:none;
}
Check fiddle
Hope it helps.

Cannot get form to validate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem is nothing validates. I can leave the form empty and it gives no alert
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function validateForm() {
var x = document.forms["myform"]["fname"].value;
if (x == null || x == "") {
alert("Enter your certificate address first.");
} else if(!x.match(/facebook/g) || !x.match(/access_token/g)) {
alert("Invalid certificate address, please try again...");
return false;
}
)
</script>
<form name="myform" action="welcome.php" method="post" onsubmit="return validateForm();">
Name: <input type="text" name="fname">
<input type="submit" value="submit">
</form>
</body>
</html>
not sure what is wrong with this. looks pretty straitforward
Missing your last end bracket. Changes ) to }

Categories