+ 2
[SOLVED] Input validation
I need to make this script work but I am confused why it doesnt work...🤔🤔🤔 The client needs to input his Username and Password. After submit the script check wether the input value is equal to a variable that is hard coded. Any help will be appericiated!🙉 https://code.sololearn.com/WIHz1GGyo63R/?ref=app
7 Answers
+ 3
Actually, let's back up a second. The [JS] tab loads in <head>, which means that your javascript runs before the DOM is ready. So, this is happening right away:
var username = document.getElementById("username");
// what did we get?
alert(username);
// output: null - whoops, the DOM isn't loaded
There are at least a couple solutions:
Use global variables, and populate them when the onload() event fires:
var username;
window.onload = function() {
username = document.getElementById(....);
alert(username); // did it work?
// should show HTMLElement
}
Or populate them in your validator:
function validate() {
username = document.getElementById(...);
alert(username) // did it work?
// ...rest of your code
}
You could also put your code in a <script> at the end of <body> (that is, after the <input> element is seen by the parser).
You should probably make one of those changes (i.e., get a working handle) before you continue debugging.
+ 3
null and Kirk Schafer,
Thank you for the additional if statement and the extended explanation! 🌴
Issue is solved and I appreciate it 😎
+ 2
What are the values of loginuser and loginpass at [JS] line 15?
Does it make sense to check if they're equal?
Is there another test that would be more appropriate here?
+ 2
https://code.sololearn.com/WSU53a0Px77w/#html
Would like to hear if this solve your problem ☺
0
I dont need to check if the variables are equal. But the inserted text of the <input>. If thats equal to my variable pass and user then alert VALID CREDENTIALS.
edit: I always get the output INVALID CREDENTIALS even when the inserted user and pass is equal to the variable user and pass....
0
Great explanation
Im on it right now🕵
0
python ?