• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Make fields required when one has a value.

 
LaTeef Lusk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.

 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing like this
Note the additional brackets surrounding the || conditions.

 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic