Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Checkform - Checking 2 fields

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checkform - Checking 2 fields

    Hi

    Firstly apologies for this question as I am sure it is really basic, however I just can't get my simple bit of code to do what I want.

    I have a HTML form that I validate the entries with a bit of Java. All works perfectly until I try and check 2 fields using an AND operator (&&).

    if (form.show.value =="Yes" && form.weeks.value == "elephant")  {
    	alert( "Please enter the number of weeks that the booking will run" );
    	form.weeks.focus();
    	return false ;
    	}

    It works if I use an OR operator (||) and works if I check either field singular. From the example I've seen on the net, the syntax looks ok...I've tried with brackets in different places, one & rather than &&, even tried a sub IF.

    No point in pointing out that I'm very new to Java...

    Many thanks in advance.

    Chris


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Checkform - Checking 2 fields

    Do not use == to compare objects as this compares references and not contents. Use the equals method instead.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Checkform - Checking 2 fields

    Quote Originally Posted by Junky View Post
    Do not use == to compare objects as this compares references and not contents. Use the equals method instead.
    if (form.show.value.equals("Yes") && form.weeks.value.equals("elephant"))  {

    Is this Java or JavaScript?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. repeat creation of text fields
    By kurt-hardy in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 12:05 PM
  2. Cannot set class fields using textInput
    By javascrub in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 04:04 AM
  3. Putting text fields in an array
    By Movies32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2010, 07:46 PM
  4. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 03:37 PM
  5. Static fields and inheritance
    By helloworld922 in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 22nd, 2010, 04:02 AM