Page 1 of 1

Compare input field with local storage variable javascript

Posted: Tue Mar 03, 2015 9:43 pm
by S Butler

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");
}


Compare input field with local storage variable javascript

Posted: Tue Mar 03, 2015 10:10 pm
by Bruce Stuart

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


Compare input field with local storage variable javascript

Posted: Wed Mar 04, 2015 4:13 am
by Yurii Orishchuk

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.