473,503 Members | 12,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

form validation and alert

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
3 1417
> 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
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
Thank you both. I have the script working now.
Elaine

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2351
by: Andrew Williams | last post by:
Does anyone know of a way to quickly validate an HTML web form? I need to make sure that certain fields on my form actually contain data and are not submitted blank. I have tried using...
6
4315
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
7
4861
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
4
1804
by: Semi Head | last post by:
Hello folks, I'm looking for a script to validate a specific number value in a standard form input field. An example would be, if someone enters a number into a form input, I want the script to...
11
2496
by: greg.scharlemann | last post by:
I've been playing with this form validation method for a while and have tried an array of things but haven't had any luck with a couple items. 1. The validateForm() function doesn't detect when...
9
4153
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
2
4854
by: this one | last post by:
I have the following code <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript"...
5
1897
by: kevinmajor1 | last post by:
Hello, all. I'm currently trying to write a script that will perform form validation upon submission. I know that more validation should be performed on the server's side...this is just a test to...
8
6679
by: M.L. | last post by:
Hello. I created a form using JS validation with the form tag as follows: <form name="form1" action="dynaform.php" method="post" onsubmit="return pvg_sub();"> The js validation script sends...
3
3279
uranuskid
by: uranuskid | last post by:
Hey folks, I was going to include a contact form on my website. Well, in the first place that seemed an easy thing to do with a form that prompts a PHP file validating the input vaiables and using...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.