Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript multiple If operators

    Hi there people

    I am new to Javascript and have come accross a problem where I need to check the value of a field on a form before it is submitted. It has to be either a whole number OR a particular text value...

    I have added the following code before the form is submitted. However even if the value of the field is a whole number or it is the correct text string, the message box still pops up and the form fails to submit!

    Code:
    	if ((document.AddNewVA.HourlyRate.value!='neg') || (isNaN(document.AddNewVA.HourlyRate.value*1)))
    	{
    		// Not the correct text OR a whole number in the HourlyRate field...
    		alert('Please enter a whole number or \"neg\" in the Hourly rate field...');
    		document.AddNewVA.HourlyRate.value = "0";
    		document.AddNewVA.HourlyRate.focus();
    		return false;
    	}
    Can anyone point me in the right direction please?

    Many thanks
    Last edited by GlynD02; 06-07-2010 at 10:33 PM.

  • #2
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by GlynD02 View Post
    Code:
    	if ((document.AddNewVA.HourlyRate.value!='neg') || (isNaN(document.AddNewVA.HourlyRate.value*1)))
    	{
    If the input is a number then it can't be 'neg' so you will get the alert, while if it is 'neg' then it can't be a number so it will alert. It needs to ask another question perhaps with a different operator

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for that RandomUser. So would another nested "If" statement be the solution then please?

    I did try some experimenting with the following code:

    Code:
    if (document.AddNewVA.HourlyRate.value!='100' || document.AddNewVA.HourlyRate.value!='200')
    	{
    	alert('Please enter 100 OR 200 in the Hourly rate field...');
    	}
    ...and I still got the prompt up even if I entered 100 or 200 into the text box...

    Cheers
    Last edited by GlynD02; 06-07-2010 at 10:34 PM.

  • #4
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by GlynD02 View Post
    Thanks for that RandomUser. So would another nested "If" statement be the solution then please?
    No

    I did try some experimenting with the following code:

    Code:
    if (document.AddNewVA.HourlyRate.value!='100' || document.AddNewVA.HourlyRate.value!='200')
    	{
    	alert('Please enter 100 OR 200 in the Hourly rate field...');
    	}
    ...and I still got the prompt up even if I entered 100 or 200 into the text box...

    Cheers
    At least one of those tests is always going to evaluate true, therefore you will always get the alert. https://developer.mozilla.org/en/Cor...ical_Operators

  • Users who have thanked randomuser773 for this post:

    GlynD02 (06-08-2010)

  • #5
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks RandomUser

    I looked at it again and changed it from a Logical OR to a Logical AND i.e.

    Code:
    if ((document.AddVA.HourlyRate.value!='Neg') && (isNaN(document.AddVA.HourlyRate.value*1)))
    Which has now done the trick...


    Cheers


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •