I spent 4 hours trying to figure out what's going on in the following code. I can hardly understand it, since I haven't programmed Javascript yet.
Javascript:
function pincheck() {
var n = 123456789123,
t = md5(n + " " + $("#wert").val()),
e = $("input[name='some_hash']").val();
$.post("https://url-test.com/site?check=" + t, {some_hash: e}, function(n) {
$("#inputWert").html(n == t ? "<span style='color:green; font-weight:bold'>OK</span>"
: "<span style='color:red; font-weight:bold'>NOT OK</span>")
})
}
Corresponding HTML-form:
<form action="https://url-test.com/site" method="post" accept-charset="utf-8">
<div style="display:none">
<input type="hidden" name="some_hash" value="2cb6beab7ac4240043b20674a3dce6a5" />
</div>
<input type="text" id="wert" name="wert" placeholder="WERT" onchange="pincheck()">
<div id="inputWert">Please input</div>
<input type="submit" value="Submit" />
<h2>Please explain<br/><textarea name="explain" style="width: 650px;height: 100px;" placeholder="Explanation"></textarea><br/>
</form>
My thoughts were:
The function pincheck() checks if n==t
But since t is a md5 hash, it
can never be equal to n, which is a digit number.
Is this correct? If wert=000, will be the value of the variable "t" an MD5 of the string "123456789123 000"? I hope I explained correct.
The background of my question refers to our university IT-security task. We have to guess some number, and then enter it into the text form, whos id should be "wert" I guess.
I hope someone can help me. Thank you in advance!
When the user types something into the WERT input field, the Javascript takes the user's input, puts 123456789123 at the beginning of it, calculates the MD5 hash of this, and assigns that to t. e is set to the contents of the hidden some_hash value.
Then it performs an AJAX query, sending t and e to the url-test.com/site script. t is sent in the check URL parameter, while e is sent in the some_hash POST data.
The server script returns back a string. If that string matches t, then it displays a green OK. If they don't match, it displays a red NOT OK.
My guess is this is part of a CAPTCHA test. The hidden input is a code that indicates which image was displayed. On the server, this code can then be used to look up the MD5 hash of the text in the image.
Simply, the Javascript is setting 3 variables (using the var keyword), then making an HTTPS POST call using jquery.
Picture the code like this line by line:
// Set n to a seemingly arbitrary number
var n = 123456789123;
// Set t to an MD5 hash using n and whatever the value of the "wert" element is
var t = md5(n + " " + $("#wert").val());
// Set e to the value of the element with a name of "some_hash"
var e = $("input[name='some_hash']").val();
// Makes a POST call to a URL built using the above variables
// Format $.post(URL, data(in JSON format), callback function)
$.post(
"https://url-test.com/site?check=" + t,
{some_hash: e},
function(n) {
// Set the HTML body of the "wert" element
// If n (returned by the POST call) is equal to t, set font color to green, otherwise set font color to red
$("#inputWert").html(n == t ? "<span style='color:green; font-weight:bold'>OK</span>" :
"<span style='color:red; font-weight:bold'>NOT OK</span>")
}
);
Let's take your code in pieces: the "pincheck" function starts out by setting three variables: n (123456789123), t. which uses the javascript MD5 hash function with an initial string of n + some spaces + whatever value you enter into the "wert" field on your form. Finally, you retrieve the value of a hidden field called "some_hash" and store that in the variable "e"/ You then initiate a post to your server passing a key-value pair of "some_hash" (the key) and the value of the hidden field. When the post returns, it calls the anonymous function at the end of the post statement, passing in the returned value from the post operation as the (local to the anonymous function) variable "n". This "n" is a different variable from the one defined at the opening of the pincheck function. The anonymous function then checks to see if the value returned from the post operation is the same as the one calculated using the javascript MD5 function. If they are the same, then the first span tag is displayed, if they are not equal, then the second span tag is displayed. Hope this helps.
Related
I have a values in google sheet and the format is =+40,-58. This give me ERROR! because the sheet is taking it as formula.
I can manually edit this by adding ' single qoute before equal sign but when i append qoute using script it append qoute with ERROR!.
Tried multiple thing like getting cell type, convert it to string.
Tried set formula method but it appends another equal sign before the cell value
please check the code below
if (//my condition){
sheet.getRange(i,col_in+1).setValue("'"+colvalue)
I am looking for possible solutions like, how can I get the actual value of the cell from fx
or
How can i append a single quote with the cell value instead of appending quote with ERROR.
please see the screenshot of the sheet
Descrition
Because the formula is giving "#ERROR" you need to getFormula and use setValue
Script
function test() {
let cell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A6");
let value = cell.getFormula();
if( value !== "" ) {
console.log("formula = "+value);
cell.setValue("'"+value);
}
}
Console.log
7:30:31 AM Notice Execution started
7:30:31 AM Info formula = =+52,-64
7:30:32 AM Notice Execution completed
This returns NaN in the browser alert:
var in1 = parseFloat(document.getElementById('input1').value);
var form = document.getElementById('formC');
form.addEventListener('submit', function() {
alert(in1);
});
However this works:
var form = document.getElementById('formC');
form.addEventListener('submit', function() {
var in1 = parseFloat(document.getElementById('input1').value);
alert(in1);
});
Could someone explain whats going on here? From the error it looks like "in1" is outside the scope of the 'function()' block however doesn't "var" make it global?
The html part:
<form id="formC">
<input type="text" id="input1">
</form>
Because in the first example, you are attempting to get the value of the input and parse the float out of it immediately (before the user has had a chance to enter any data into it). So, trying to parse a float out of an empty string will yield NaN.
But in the second, you wait until the form is submitted to get the data, which is after the user has entered some data into the input.
On page render the text field is blank.
Yes, and that is when this runs:
var in1 = parseFloat(document.getElementById('input1').value);
and sets the value of in1 to NaN.
But in both cases I manually type a number (5 or 3) into the text
field before clicking the submit button.
In the first case, it doesn't matter what you do because the variable in1 already has its value. It's too late, that line has already been executed. You have no code that updates it to use the number you entered.
In the second case, it works because the variable isn't attempting to get the value until you've submitted the form, which is after you entered some data in the input field.
When the page is rendered I am assuming there is no value in the input. And you already calculated the value of it and just using on submit.
But in the second case you are reading the live value of the input
Sushanth -- and Scott Marcus are correct, you can verify by making following changes in your code
var form = document.getElementById('formC');
var in1 = document.getElementById('input1');
form.addEventListener('submit', function() {
alert(parseFloat(in1.value));
});
as you can see in1 is accessible inside the function, you just need to read the value once you submit, not before.
Above code should not return NaN unless provide invalid float number.
I'm trying to capture the value of a text field on an HTML form using document.getElementById(my_field).value where the variable my_field is passed to my function dynamically, but am hitting a wall.
How do you use a variable in this context?
The function just doesn't seem to parse the contents of the variable my_field, instead treating it as a string no matter whether I use quotes, square brackets or curly braces.
function myFunction() {
var my_field = arguments[0];
var current_value = document.getElementById(my_field).value;
alert ("Current Value: " + current_value);
}
I'm doing it this way because I have multiple records on a form and each row has its own unique id for the required field.
Running the above just does nothing. The alert never pops which I assume is because current_value never gets set.
To add further detail - I tried to simplify everything for the purposes of this question as there's lots of other unnecessary complications that will only detract from the main issue - on my HTML form is a text field which calls my function on onChange
onchange="enforce_multiples('quantity[<?php echo $line_id; ?>]',<?php echo $product['minimum'];?>)"
I've checked that arguments[0] and [1] are being captured correctly by outputting their values to an alert. Everything works fine up until I try to set the quantity_entered value.
<script>
function enforce_multiples() {
var line_id = arguments[0];
var quantity_increments = arguments[1];
var quantity_entered = document.getElementById([line_id]).value;
alert("QE" + quantity_entered);
//var quantity_mod = quantity_entered % quantity_increments;
//var revised_quantity = quantity_entered - quantity_mod;
//alert("RQ: " + revised_quantity);
//document.getElementById([line_id]).value = revised_quantity;
}
</script>
Checked the console and I receive the error: Uncaught TypeError: Cannot read property 'value' of null on the geElementById line
You should write document.getElementById(my_field) instead of document.getelementbyid(my_field).
OK so I got to the bottom of this in case anyone is interested.
In order to use a variable in document.getElementById() you simply add the variable name with no quotes.
var my_variable = "field1";
document.getElementById(my_variable);
The reason this wasn't working on my form was because the text fields only had the name parameter and not an id parameter.
So I needed to change:
<input type="text" name="field_name" value="1234" />
To
<input type="text" name="field_name" id="field_name" value="1234" />
And that sorted it. Otherwise I was just getting generic NULL error messages in the console.
Is there any smart, minimalistic way to always show the prefix (positive, negative) of a number in a HMTL input field? (e.g. +1, 0, -1)
I have only found s solution for PHP:
How to prefix a positive number with plus sign in PHP
I have to use <input type="text"> since there are different implementations for text=number in different browsers: Localization of input type number
Why am I doing this?
I have an input field that shows the percentage that can be added (or subtracted) to a certain value.
Basevalue: 10
Mofification %: +10
Results: 11
The easiest way would be using some basic javascript. Add a script at the end of your HTML page (before the body closing tag) then give to the input an id, for example prefixedInput. Then you can write your little script
var inputField = document.getElementById("#prefixedInput");
var inputFieldValue = inputField.value;
if (inputFieldValue > 0) {
inputField.value = "+" + inputFieldValue;
}
if (inputFieldValue < 0) {
inputField.value = "-" + inputFieldValue;
}
Now, that works in a way that isn't really useful because this function will be executed just one time when the page will load, so if you have assigned to your input a value, this will be prefixed with its sign. However if you want to bind this behaviour to some actions (e.g. prefixing the value even if the user inserts the value after the intial page load) you will be forced in using event listeners.
I need to make a calculator using JavaScript, HTML and CSS. Everything worked out fine until I came to coding the compute (=) button.
This is my HTML:
<input type="button" value=" = " onclick="compute()">
This is my JS:
function compute() {
var input_var = document.getElementById('input');
ans = Math.floor(+eval(input_var.value));
document.getElementById('answer').value = '=' + 'ans';
}
If anyone that knows how to solve what's wrong, I would greatly appreciate it if you could reply.
First of all, you should post the whole code to get accurate solution!
Probably these could be some of the errors:
Set id attribute of your = button with value "input"
3rd line should be: ans = Math.floor(eval(+input_var.value));
4th line should be: document.getElementById('answer').value = '=' + ans; as StaticBeagle has also mentioned.
You should be lucky that you made the mistake to put the variable in quotes. That's why you don't get a value other than the literal string =ans(maybe, we don't know as you didn't post all code that's needed to give a better answer).
Back to why you're lucky.
Never use eval! eval is evil. (Unless you know what you do, but you don't the next couple of years). To parse a number, you'd use Number(input_var.value).
The next error is that you create a global variable by omitting one of var, let, const for your ans declaration.
The next thing you shouldn't do is to use inline javascript. We use eventListener instead. As said before, it's impossible to answer more specific as your question lacks too many details - however I'll show you how you get a value by pressing a button in the console.
document.getElementById('foo').addEventListener('submit', e => {
// prevent submitting the form (I guess another error in your code)
e.preventDefault();
const value = Number(document.getElementById('input').value);
console.log('The value is: ' + value);
}, false);
<form id="foo">
<input type="number" id="input">
<input type="submit" value=" = ">
</form>
Not sure if ans is a local or global variable, but if its intention is to be a local variable then you should have it like this:
var ans = Math.floor(eval(+input_var.value));
Also, because you're setting the value of your element to '=' + 'ans' you're actually setting it to the actual string 'ans'. If you want to refer to what ans is you should write it like this:
document.getElementById('answer').value = '=' + ans;