Hi there,

I have two prompt dialogue boxes that ask the user the enter their name and their employee number.

I am experiencing two errors. The first is, if a user enters their name incorrectly, using numbers for example, there is an error message that asks the user to re-enter their name using a string. If the user ignores the error message and enters a number a second time, the program accepts the entry, which it shouldn't.

The second error has to do with the employee number prompt. The prompt asks the user to enter an employee number that is equal to 6 digits, the program however accepts any number, less than or greater than 6 digits, which it shouldn't.

I'm very new at this and desperate to learn what I'm doing wrong. Can someone out there help me to clean up my code? I don't want the user to get away with entering anything incorrectly.

PLEASE PLEASE HELP

<script type="text/javascript">

// prompt the employer the enter the employee name and number

var employName = prompt ("Please enter the employees' name" , " ");
if (isNaN(employName)) {
 document.write("You are enquiring about: " +employName ); 
} else { (employName == null || !isNaN(employName)) 
employName = prompt ("Please enter a valid name in this field");
document.write("You are enquiring about: " +employName) ; }

document.write("</br>");
	

var employNum = prompt ("Please enter the employees' 6 digit employee number" , " ");
employNum = parseInt(employNum); {
if (employNum.toPrecision(6)) {
document.write("Employee Number: " +employNum);    
} else { (isNaN(employNum) || !(employNum.toPrecision(6)) ) 
       employNum = prompt("Enter a 6 digit employee number, using only whole numbers ", "");  
	   document.write("Employee Number: " +employNum); }
  		
document.write("</br>");
}
</script>

Recommended Answers

All 4 Replies

newbiwJS,

There's a nmber of ways to write the code.

Here's a simple version:

var ask = true; 
while(ask){
	var employName = prompt ("Please enter the employee's name" , "");
	if (employName === null) {//user cancelled
		ask = false;
		break;
	}
	else if (employName !== '' && isNaN(Number(employName))) {
		document.write("You are enquiring about: " + employName + "<br />");
		break;
	}
}
while(ask){
	var employNum = prompt ("Please enter the employee's 6 digit employee number" , "");
	if (employNum === null) {//user cancelled
		ask = false;
		break;
	}
	else if (employNum.length == 6 && !isNaN(parseInt(employNum))) {
		document.write("Employee Number: " + employNum + "<br />");
		break;
	}
}

This version is more fun and would be better if you needed many prompts :

var employName, employNum;

// Set up a series of prompts, default responses, validation functions and action functions
var dialogData = [
	{
		cue: "Please enter the employee's name",
		r: "",//default response
		validateFn: function(r){ return r !== '' && isNaN(Number(r)); },//returns true if successful, otherwise false
		actionFn: function(r){//what to do if successful
			employName = r;
			document.write("You are enquiring about: " + r + "<br />");
		}
	},
	{
		cue: "Please enter the employee's 6 digit employee number",
		r: "",//default response
		validateFn: function(r){ return r.length == 6 && !isNaN(parseInt(r)); },//returns true if successful, otherwise false
		actionFn: function(r){//what to do if successful
			employNum = r;
			document.write("Employee Number: " + r + "<br />");
		}
	}
];

/*
 * promptDialog({cue, r, validateFn, actionFn});
 *  This function will repeatedly prompt the user 
 *  until either a valid response is entered 
 *  or the prompt dialog is cancelled.
 */
function promptDialog(data) {
	var response = data.r;
	while(true) {//prompt repeatedly until use provides a valid response or cancels
		var response = prompt (data.cue, response);
		if (response === null) { break; }//user cancelled
		if (!data.validateFn || typeof data.validateFn !== 'function') {
			return response;//assume OK if no validate function is specified
		}
		if (data.validateFn(response)) {
			if (data.actionFn && typeof data.actionFn == 'function') {//if an action is specified
				data.actionFn(response);//perform the action
			}
			return response;
		}
	}
	return false;//return false if we broke out of the while loop
}

var go = true;//flag that determines whether to continue looping through dialogData.
for (var i=0; go && i<dialogData.length; i++) {
	go = promptDialog(dialogData[i]);
}

Note that in javascript, functions are "first class objects" enabling us to eg. set them as properties of other objects and pass them as arguments to other functions.

Airshow

Thank you so much for the detailed response, I especially appreciate the comments. Being new at this I am able to follow along and have certainly learned some techniques not yet introduced to me.

Thank you again, I hope to return to favor one day.

I am extremely new too and bless your heart for sharing such instructions and examples for us "newOnes" :-)

Rather than all this isNaN and parseInt usage (especially for testing that a string is valid alphabetic) you should utilize regular expressions. Regex is very powerful and will serve you well to begin learning how they work.

Match exactly 6 numeric digits:

var string = "123456";
var regexp = /\d{6}/;

alert( regexp.test(string) );

Match only alphabetic characters:

var string = "Abcdefghijk";
var regexp = /^([a-z]+)$/i;

alert( regexp.test(string) );

There are lots of nifty regular expressions cheatsheets on the internet, it's a learning curve, but well worth it. They are a very powerful weapon in the arsenal of any programmer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.