aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Make fields required when one has a value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Make fields required when one has a value." Watch "Make fields required when one has a value." New topic
Author

Make fields required when one has a value.

LaTeef Lusk
Greenhorn

Joined: Mar 28, 2009
Posts: 15
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

Joined: Jan 10, 2002
Posts: 61662
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kiran Joshi
Ranch Hand

Joined: Sep 04, 2005
Posts: 54
Try doing like this
Note the additional brackets surrounding the || conditions.

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make fields required when one has a value.