• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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: 67750
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

 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic