/    Sign up×
Community /Pin to ProfileBookmark

Prompt and validate entry

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 an employee number that is less than 6 digits, the program accepts the number, which it shouldn’t.

The second error is that the user is reminded only once to correct their error. If for example they enter a number when they are asked to enter a string. An error message will appear, but only once, and the second time through the program accepts whatever the user enters.

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 ?

[CODE]
<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>

[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@trandrusJan 30.2011 — [CODE]<script type="text/javascript">

function valReq(val) {
return (val);
}

function valString(val) {
return (isNaN(val));
}

function valNum(val) {
return (!isNaN(val));
}

function valLen(val, len) {
val = val+'';
return (parseInt(val.length)==parseInt(len));
}

var employeeName, valid=false;
while(!valid) {
employeeName = prompt("Please enter the employees' name" , "");
valid = (valReq(employeeName) && valString(employeeName));
}

var employeeNum, valid=false;
while(!valid) {
employeeNum = prompt("Please enter the employees' 6 digit employee number" , "");
valid = (valReq(employeeNum) && valNum(employeeNum) && valLen(employeeNum, 6));
}

document.write("You are enquiring about: " +employeeName+"<br />");
document.write("Employee Number: " +employeeNum+"<br />");

</script> [/CODE]
Copy linkTweet thisAlerts:
@newbieJSauthorJan 30.2011 — Thank you for taking the time to reply. I will try your code. It would be extremely helpful if you included a few comments as I really am very new at this and learning a new technique every step of the way.

I appreciate the help very much.
Copy linkTweet thisAlerts:
@trandrusJan 31.2011 — [CODE]<script type="text/javascript">

/******
*Functions used for validating form entries
*/

function valReq(val) {
return (val);
}

function valString(val) {
return (isNaN(val));
}

function valNum(val) {
return (!isNaN(val));
}

function valLen(val, len) {
val = val+'';
return (parseInt(val.length)==parseInt(len));
}


/******
*Prompt the user for their name until they enter a valid response
*/

var employeeName, valid=false;
while(!valid) {
employeeName = prompt("Please enter the employees' name" , "");
valid = (valReq(employeeName) && valString(employeeName));
}

/******
*Prompt the user for their employee number until they enter a valid response
*/

var employeeNum, valid=false;
while(!valid) {
employeeNum = prompt("Please enter the employees' 6 digit employee number" , "");
valid = (valReq(employeeNum) && valNum(employeeNum) && valLen(employeeNum, 6));
}

/******
*Output user responses
*/

document.write("You are enquiring about: " +employeeName+"<br />");
document.write("Employee Number: " +employeeNum+"<br />");

</script>[/CODE]
Copy linkTweet thisAlerts:
@KorJan 31.2011 — 
return (parseInt(val.length)==parseInt(len));
[/quote]

In you example [B]parseInt()[/B] is redundant. Both values are integer numbers anyway.

And be careful with parseInt(). The full syntax is parseInt(string,base) and it is better to specify the base, if you want to avoid unexpected results
<i>
</i>alert(parseInt('08')); // returns 0, as the string is considered an octal
alert(parseInt('08',[COLOR="Blue"]10[/COLOR])) // returns the expected 8, as the decimal base is now correctly specified
Copy linkTweet thisAlerts:
@MikeOSJan 31.2011 — To do this I would use a post test loop with regular expressions like so:

[CODE]var userName, userNo, regEmployeeNo, regUserName, regWhiteSpace;

regUserName = /^[a-z ]+$/i;
regWhiteSpace = /^s+$/;
do
{
userName = prompt("Please enter your name","");
}
while(userName == null || !regUserName.test(userName) || regWhiteSpace.test(userName));

regEmployeeNo = /^d{6}$/;
do
{
userNo = prompt("Please enter your six digit employee number","");
}
while(!regEmployeeNo.test(userNo) || userNo == null);

document.write("Your name is " + userName + " and your employee number is " + userNo);[/CODE]


Firstly I declare five variables (to be honest I'd ordinarily use objects to prevent global bloat but for simplicity I'll avoid that). userName and userNo will hold whatever the user enters into the prompt dialog boxes. regUserName is a regular expression that only allows the characters a-z, A-Z (note the i at the end of the regular expression means case insensitive), and white spaces. regWhiteSpace holds another regular expression which ensures the user can't just enter white space(s) only. I use both of these regular expressions to test the userName input to ensure it's valid. It may be possible to combine these regular expressions but my head always hurts when I do regular expressions. Following this is a do while loop where these two regular expressions will be tested, note that I'm using a do while loop as the code (the prompt dialog box) must always run at least once in both cases, doing it with this loop ensures the condition is only tested after the user has had a chance to input something. Now once the user has entered something the conditions are tested, if all is well then the data is stored in the userName variable, if all is not well then the loop loops and the user is given another opportunity to enter the correct data ad-infinitum. When they do enter the correct data the next prompt dialog box will appear for the employee number. In this case I've created a regular expression so that only numbers are allowed to be entered and only six numbers in total, no more no less. Again if they enter the right data all is well, if not they get another opportunity to do so ad-infinitum. Finally I simply print whatever they entered out to screen, I'm sure you can modify the output to your liking. Hope this helps.

Just to add you can allow other characters in the user name by adding them between the brackets in the regular expression, for example some people have an apostrophe in their name so you may want to allow that character, simply add it to the acceptable characters list so to speak and all should be well.
Copy linkTweet thisAlerts:
@trandrusFeb 01.2011 — thanks for the feedback ?
×

Success!

Help @newbieJS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...