[SOLVED] Input validation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

17th Aug 2018, 5:11 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
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.
17th Aug 2018, 5:39 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
null and Kirk Schafer, Thank you for the additional if statement and the extended explanation! 🌴 Issue is solved and I appreciate it 😎
17th Aug 2018, 6:21 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
+ 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?
17th Aug 2018, 5:21 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
https://code.sololearn.com/WSU53a0Px77w/#html Would like to hear if this solve your problem ☺
17th Aug 2018, 5:59 PM
null
null - avatar
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....
17th Aug 2018, 5:23 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
0
Great explanation Im on it right now🕵
17th Aug 2018, 5:53 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
23rd Aug 2018, 4:07 PM
zombie05
zombie05 - avatar