S Butler
Posts: 0
Joined: Mon Nov 17, 2014 4:44 pm

Compare input field with local storage variable javascript

Hi,

I have written some javascript to compare the text of a input component to that of a local storage variable when a button is clicked and either navigate to a page if successful or generate an alert if unsuccessful. The local storage variable is set to "Hospital1" and when I enter "Hospital1" into the input component, I continue to get my alert that the values do not match. I've added my javascript below. Can you please let me know if you see any issues? Thanks!

var password = localStorage.getItem('hospitalId');
var input = Apperyio('input').val();
if(input == password){
Apperyio.navigateTo('ipadscreen1', {});
}
else
{
alert ("The password you entered is incorrect. Please try again");
}

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Compare input field with local storage variable javascript

You might want to try to trim() both values before doing the comparison ... Or even as part of the comparison ...

And .. Unless you are really concerned with case ... Taking both values to lower case might also be useful ... With toLowerCase() ( check my caps on w3schools.com ) ...

Finally ... Why not take your validation code... And put it in a function call in a project specific piece of JavaScript ... And call the function from your button ... That way you can use the debugger to see what's really going on here ... In case .trim().toLowerCase() doesn't cut it ?

If you need help with that ... Let me know...

Best,
Bruce

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Compare input field with local storage variable javascript

Hello,

Please try to see what you have in your variables.

Modificate your code with following one:

pre

var password = localStorage.getItem('hospitalId');
var input = Apperyio('input').val();

alert("password = '" + password + "', input = '" + input + "'");

if(input == password){
Apperyio.navigateTo('ipadscreen1', {});
}
else
{
alert ("The password you entered is incorrect. Please try again");
};

/pre

Regards.

Return to “Issues”