How to alert when user enters nothing? - javascript

I'm making a JavaScript project and I want to alert when the user enters nothing in the prompt
function verifyage() {
var age = prompt("enter your age:", "");
var status = (age >= 18) ? "adult" : "minor";
if(status == "minor") {
alert("you are too young");
} else if (status == "adult") {
alert("you're in");
} else if (status == "null") {
alert("you need to enter your age");
}
else {
void(0);
}}
<button onclick="verifyage();" type="button">watch movie</button>
What it's supposed to do:
define a variable age which defines the age of user
define a variable status which has two value adult and minor
prompt for the age of the user
alert the message you are too young when the user enter a number less than 18
alert the message you're in when user enters number greater than 18
alert the message you have to enter your age when user enters nothing
But when I test it and enter nothing it says you are too young - why?
I tried how to show alert when user enter other then number value in textfield and other but nothing.
EDIT:When i press cancel it says you are too young...help?

It seems to me that you want to continue to prompt the user to enter something into the field. To accomplish that I would have a while loop that checks to make sure the age is not blank. It also looks unnecessary to set status to then just check for the value when you could just check the age.
function verifyage() {
var age = prompt("enter your age:", "");
//var status = (age >= 18) ? "adult" : "minor";
while(age === "" || isNaN(age)){
age = prompt("enter your age:", "");
}
if(age <= 18) {
alert("you are too young");
} else if (age > 18) {
alert("you're in");
}
else {
void(0);
}
}

function verifyage() {
var age = prompt("enter your age:", "");
if(isNaN(parseFloat(age))) { // check: if user entered a number/string/blank
alert("You entered nothing");
}
else {
var status = (age >= 18) ? "adult" : "minor";
if(status == "minor") {
alert("you are too young");
} else if (status == "adult") {
alert("you're in");
}
}
}
verifyage();

I am no javascript expert, but looking at your code I think "null" will not do what you want it to. Right now it is explicitly looking for the typed text "null". In c++ for example you could use
else if (status == "")
or (maybe even better) just use else (so that everything except for adult/minor could give an error)
Also I would not make the comparison with the variable "status", but with "age". This should be the first comparison you make. When the typed age == "" ( <- nothing) then you could give an alert, prompting the user to type their age again.
Good luck!

Please see the below correction, You were running into trouble since when you do enter, the value of your age variable was 0.
function verifyage() {
var age = prompt("enter your age:", "");
var status = (age >= 18) ? "adult" : "minor";
if (age == 0)
status = "null";
if(status == "minor") {
alert("you are too young");
} else if (status == "adult") {
alert("you're in");
} else if (status == "null") {
alert("you need to enter your age");
}
else {
void(0);
}}
<button onclick="verifyage();" type="button">watch movie</button>

function verifyage() {
var age = prompt("enter your age:", "");
if(age=='' || age<="0" || isNaN(age)){alert ("you have to enter your age");
}
var status = (age >= 18) ? "adult" : "minor";
if(status == "minor") {
alert("you are too young");
} else if (status == "adult") {
alert("you're in");
} else if (status == "null") {
alert("you need to enter your age");
}
else {
void(0);
}}
First of all if you don't select the age it will assign it to 0 which is under 18 the first if will obtain so to avoid that add if statement to validate if the user don't write anything or write 0 by error like the code above

Related

JavaScript: add alert message if name or password less than 6

try to add function in JS where alert box will show message Password or Username must be more than 6 :
Password :
Password must have atleast 6 characters
Username :
Username must have atleast 6 characters
So far I got function show alert if Pass or Username is NULL
JaveScript Code :
function validateForm() {
var x = document.forms["myForm"]["namapengguna"].value;
if (x == "" || x == null) {
alert("Nama must be filled out");
return false;
}
var x = document.forms["myForm"]["username"].value;
if (x == "" || x == null) {
alert("User must be filled out");
return false;
}
var x = document.forms["myForm"]["password"].value;
if (x == "" || x == null) {
alert("Pass must be filled out");
return false;
}
var x = document.forms["myForm"]["confirm_password"].value;
if (x == "" || x == null) {
alert("Confirm Pass must be filled out");
return false;
}
else {
alert('Application Has Been Registered');
location.assign("homeA.php");
}
}
but I couldn't figure out how can add alert message if name or password less than 6.
Any idea or solution are really appreatiate.
p/s: if my question is not good enough for you to understand, please tell me so I can improve it.
You can check string length by doing x.length. So:
if (x.length <= 6) {
// Do what you want
}
You may also want to make sure the user hasn't inputted any spaces at the start or end of the field.

how to tell javascript that the user enter a string in the prompt box and work with it in if statment

how to tell javascript that the user enter a string in the prompt box and work with it in an if statment ? If the user enters a string of letters I want it to alert "You did not enter a number", and if they entered a string if digits then continue with the logic.
var userGess = prompt("guess a number");
var secretNumber = 7;
if (Number(userGess) > secretNumber) {
alert("the number is to high")
} else if (Number(userGess) < secretNumber) {
alert("the number is to low")
} else if (Number(userGess) == secretNumber) {
alert("you are correct")
} else if (userGess == toString(userGess)) {
alert("you didnt type a number")
}
You can use isNaN(userGess) to check if a given string userGess is non-numeric.
However, that returns false if userGess is empty string, so you have to explicitly check it. So your final condition becomes
userGess === "" || isNaN(userGess)
var userGess = prompt("guess a number");
var secretNumber = 7;
if(userGess === "" || isNaN(userGess)) {
alert("You didn't enter a number")
} else if (Number(userGess) > secretNumber) {
alert("the number is to high")
} else if (Number(userGess) < secretNumber) {
alert("the number is to low")
} else if (Number(userGess) == secretNumber) {
alert("you are correct")
}

How do I find and use what a function has alerted in Javascript?

I am making a little 'guess the number' game, and I need to find a way to repeat the function based on what it outputs. What I have right now is this:
var input = prompt("Please enter a number between 1 and 100!");
var ranNum = Math.floor(Math.random()*100);
function numCheck()
{
if (input < 1)
alert("Please enter a valid number!");
else if (input > 100)
alert("Please enter a valid number!");
else if (input > ranNum)
alert("Try a little lower!");
else if (input = typeof stringValue)
alert("That is not a number!");
else if (input < ranNum)
alert("Try a little higher!");
else
alert("You got it!");
}
numCheck();
I did this using the inspect option on chrome on the page about:blank. Now that I have the function, I think I need to use another if else statement. It would detect if numCheck() output is You got it!" and if it didn't, it would replay the numCheck function, all the way until the player got the correct number.
How would I do it like that? And if there is a simpler way, what is it?
I made a fiddle that will use alerts and prompt accessible inside the page (I am not sure this what you asked for or not - explain more if not):
HTML:
<div class="numberguess">
<input type="text" id="entry" value="" />
<input type="button" id="send" class="" value="submit" />
</div>
<div id="alert"></div>
JS:
var _alert = document.getElementById('alert');
var ranNum = Math.floor(Math.random()*100);
function numCheck(){
var input = document.getElementById('entry').value;
if (input < 1 && input > 100)
_alert.innerText = "Please enter a valid number!";
else if (input > ranNum)
_alert.innerText = "Try a little lower!";
else if (typeof input != 'string')
_alert.innerText = "That is not a number!";
else if (input < ranNum)
_alert.innerText = "Try a little higher!";
else
_alert.innerText = "You got it!";
}
numCheck();
var el = document.getElementById('send');
el.addEventListener('click', function(){
numCheck();
}, false);
Check this as live exp: Jsfiddle
you can use looping like this code below
var input; // var to hold user input
var ranNum = Math.floor(Math.random()*100);
var win = false; // var if we want to ask another input
var trycount = 0; // don't want to ask for input forever
function numCheck()
{
if (input*1 != input) // check if number first
alert("That is not a number!");
else if (input < 1) // check lower bound
alert("Please enter a valid number!");
else if (input > 100) // check upper bound
alert("Please enter a valid number!");
else if (input > ranNum) // tell a hint
alert("Try a little lower!");
else if (input < ranNum) // tell a hint
alert("Try a little higher!");
else{
alert("You got it!");
win = true; // end the user prompt
}
}
while (!win && trycount < 3){ // while wrong guesses and below 3 tries
input = prompt("Please enter a number between 1 and 100! (" + ranNum + ")");
trycount++; // count the try
numCheck();
}
You need a looping mechanism that will keep prompting the user to input another value.
var ranNum = Math.floor(Math.random()*100);
function numCheck()
{
var input = prompt("Please enter a number between 1 and 100!");
if (input === null)
return false; // user pressed "cancel"
if (input == ranNum) {
alert("You got it!");
return false;
}
if (input < 1)
alert("Please enter a valid number!");
else if (input > 100)
alert("Please enter a valid number!");
else if (input > ranNum)
alert("Try a little lower!");
else if (input < ranNum)
alert("Try a little higher!");
else
alert("That is not a number!");
return true; // continue looping
}
while(numCheck());

Checking for no input in control structure

I am writing this basic control structure for a lesson and I am getting some unexpected behavior.
var answer = prompt('what is your age');
if (answer >= 21) {
alert('good to go!');
}
else if (answer < 21) {
alert('sorry not old enough');
}
else if (answer != typeof Number) {
alert('please enter your age as a number');
}
else if (answer === null) {
alert('you did not answer!');
}
On the very last conditional, I would expect that if I left the prompt empty, it would execute the last alert. However, it just says 'not old enough'. Is it treating no input into the prompt as 0? How can fix this?
Thanks.
Prompt doesn't return null if the user hits OK, to test for emptiness, you need to check if the string is empty answer === ""
You need to move the last two checks to the top since "" < 21 is true:
var answer = prompt('what is your age');
if (answer === '') {
alert('you did not answer!');
} else if (isNaN(answer)) {
alert('please enter your age as a number');
} else if (answer >= 21) {
alert('good to go!');
} else if (answer < 21) {
alert('sorry not old enough');
}

Javascript: Validation alerts 2

My goal is this:
Check if email and name are empty. If so, give 'Enter email or name' alert.
If they do, check for an # in email If none is found, give 'Bad email' alert.
Check if email and name contain any letters, if they do, give 'Success' alert
function test(email, name){
if(email=="" || name == "") {
alert("Enter mail or name");}
return false;
if(email.indexOf("#") == -1){
alert("Bad email");}
return false;
var a = email.length;
var b = name.length;
if(a==>0, b==>0){
alert("Message sent");}
return true;
}
This is what I've come up with so far, but it isn't working. I'm quite new at javascript so maybe you guys could tell me what I've done wrong?
The problem you're having is the close bracket is in the wrong place. You have it at the end of your alert statement and you probably want the return to be included with your if statement. if this is the case then change it to be:
function test(email, name){
if(email=="" || name == "") {
alert("Enter mail or name");
return false;
}
if(email.indexOf("#") == -1){
alert("Bad email");
return false;
}
var a = email.length;
var b = name.length;
if(a > 0 && b > 0){
alert("Message sent");
return true;
}
}
A better way to do the same thing would be because that way you're not checking the variables for length and size twice:
function test(email, name) {
var a = email.length;
var b = name.length;
if ( a > 0 && b > 0 ) {
// ignore 0 because email addresses shouldn't start with #
if ( email.indexOf("#") > 0 ) {
alert("Message sent");
return true;
}
else {
alert("Bad email");
return false;
}
}
else {
alert("Enter mail or name");
return false;
}
}
Try this JSFiddle that seems to fit your needs http://jsfiddle.net/9nF5W/
function test(email, name) {
if (email == "" || name == "") {
alert("Enter mail or name");
return false;
}
if (email.indexOf("#") == -1) {
alert("Bad email");
return false;
}
var a = email.length;
var b = name.length;
if (a > 0 && b > 0) {
alert("Message sent");
}
return true;
}
test('tes#t', 'test');
I think there is an other mistake than the returns statements in "if(a==>0, b==>0){" by the way.

Categories