I am trying to get username for a site using the javascript prompt. If the user presses the cancel the prompt should come up again or if he enter empty value, the prompt should come up again.
I don't want the user to use the site without entering the proper username. for that I wrote a function to detect if username is entered or not. But it is not working correctly, it returns the value for the first time, but If i press cancel and enter the value in the second prompt. that function returns nothing. I don't understand what I did wrong.
function getusername()
{
var user = prompt("Please Enter Your Username");
if(user == null || user == "")
{
getusername();
}
else
{
return user;
}
}
when I call this like alert(getusername());
the value comes in the first time, but if I press cancel in the first prompt and enter username in the second prompt, the alert is undefined. Please help me.