By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,629 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,629 IT Pros & Developers. It's quick & easy.

form validation and alert

P: n/a
I can't seem to get this to work. Any help appreciated. The alert
message always comes up.

Here is my code

<SCRIPT type="text/javascript">
function fte()
{
var hours = document.form1.hours.value //get user input for hours
var weekdays = document.form1.weekdays.value //get user input for days
in month
var rlength=2;
var result =
Math.round(hours/(weekdays*8)*Math.pow(10,rlength))/Math.pow(10,rlength);

if (document.form1.weekdays.value != 20 || 21 || 22 || 23)
{
alert ("The days in the month must be either 20, 21, 22, or 23");
}
else
{

alert ("Your fte is " + result);
}
}
</script>

<form name="form1">
Enter your hours: <input type="text" name="hours"> <br>
Enter the number of work days in the month: <input type="text"
name="weekdays"> <p>
<input type="submit" value = "Calculate" onClick="fte(this)"><p>
</form>
</BODY>
</HTML>

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> if (document.form1.weekdays.value != 20 || 21 || 22 || 23)
{
alert ("The days in the month must be either 20, 21, 22, or 23");
}
else
{

alert ("Your fte is " + result);
}
}


var days = +document.form1.weekdays.value;
if (days != 20 && days != 21 && days != 22 && days != 23) {
alert("The days in the month must be either 20, 21, 22, or 23");

http://www.crockford.com/javascript
Jul 23 '05 #2

P: n/a
el*****@ori.org wrote:
I can't seem to get this to work. Any help appreciated. The alert
message always comes up.

Here is my code

<SCRIPT type="text/javascript">
function fte()
{
var hours = document.form1.hours.value //get user input for hours
You really should validate whatever is entered to make sure it
fits what you want (decimal hours? hours:minutes? integers?):

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>
var weekdays = document.form1.weekdays.value //get user input for days
in month


And here. Better to validate immediately, why bother going any
further if the input is incorrect? This is a simple test best
done using a regular expression:

if ( ! /^2[0-3]$/.test(weekdays.replace(/\s+/g,'')) ) {
// error alert

This will remove any spaces and test if the entered text
contains a 2 followed by 0, 1, 2 or 3. The test is negated so
anything that does not fit your specified range will return true
and the error alert will be generated.

Your if statement to test the input has syntax errors, a correct
version would have been:

if ( weekdays != 20 || weekdays != 21 ||
weekdays != 22 || weekdays != 23) {
// error alert
}

or perhaps:

if ( weekdays <= 19 || weekdays >= 24 ) {
// error alert
}

But as noted above, the regexp will do a better job faster. A
similar technique can be used to test the first value entered
also.

--
Fred
Jul 23 '05 #3

P: n/a
Thank you both. I have the script working now.
Elaine

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.