Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Make fields required when one has a value.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a series of input field in a from. However I have 3 fields that need to be required if one of them has a value. If all 3 are blank I do not want them to be required. The problem I am having is if I leave the fields blank and sumbit my form its telling me I have a value in one of the fields and that the other 2 are required. My code is below. Can someone help me please. thanks!

<code>
<script language="javascript">
function validatePatientSearchForm()
{

if (document.getElementById('LAST_NAME').value != "" && document.getElementById('FIRST_NAME').value == "" || document.getElementById('BIRTH_DT').value == "")
{
alert("You entered a last name. You must also enter in a first name and date of birth.");
return false; }

if (document.getElementById('LAST_NAME').value == "" || document.getElementById('FIRST_NAME').value == "" && document.getElementById('BIRTH_DT').value != "")
{
alert("You entered a date of birth. You must also enter in a first name and last name.");
return false; }

if (document.getElementById('LAST_NAME').value == "" || document.getElementById('BIRTH_DT').value == "" && document.getElementById('FIRST_NAME').value != "")
{
alert("You entered a first name. You must also enter in a last name and date of birth.");
return false; }

}
</script>
</code>
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try doing like this
Note the additional brackets surrounding the || conditions.

 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

LaTeef Lusk wrote:The problem I am having is if I leave the fields blank and sumbit my form its telling me I have a value in one of the fields and that the other 2 are required.


Which alert message you got? Try to look into that code to check why the condition gets true.
Also, cover OR boolean expression, like

 
Happily living in the valley of the dried frogs with a few tiny ads.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic