Connecting Tech Pros Worldwide Forums | Help | Site Map

Validate input if options combo selected

mariodavi's Avatar
Newbie
 
Join Date: Feb 2008
Location: Jampa - Brasil
Posts: 8
#1: Mar 5 '08
Hi everybody,

how to validate input text in the follow:

if option combo = 1,2 e 3, input 1 and input 2 must be filled, if one of this values not selected in combo (to be others), filled input 3.

validate code with error below:

Expand|Select|Wrap|Line Numbers
  1. funcion validate () {
  2. ...
  3. valor = document.form.select.value;
  4.  
  5. if (value === "1" || value === "2" || value === "3"){
  6.     alert("Input 1 must be filled.");
  7.     document.form.input1.focus();
  8.     return false;
  9. } else if (input2 === "") {
  10.     alert("Input 2 must be filled.");
  11.     document.form.input2.focus();
  12.         return false;
  13. } else {
  14.     alert("Input 3 must be filled.");
  15.     document.form.input3.focus();
  16.         return false;
  17. }
  18.     return true;
  19. }
  20.  
Thanks!

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Mar 6 '08

re: Validate input if options combo selected


Quote:

Originally Posted by mariodavi

if option combo = 1,2 e 3, input 1 and input 2 must be filled, if one of this values not selected in combo (to be others), filled input 3.

Does this mean that if the combo box has options 1, 2, or 3 selected, you want to validate that inputs 1 and 2 are filled. If none of these are selected, then input 3 has to be filled. Is that correct?
mariodavi's Avatar
Newbie
 
Join Date: Feb 2008
Location: Jampa - Brasil
Posts: 8
#3: Mar 6 '08

re: Validate input if options combo selected


Quote:

Originally Posted by acoder

Does this mean that if the combo box has options 1, 2, or 3 selected, you want to validate that inputs 1 and 2 are filled. If none of these are selected, then input 3 has to be filled. Is that correct?

The combo box has 5 options, if one of this options (1,2,3) will selected then input text 1 and 2 must be filled (for the user). If selected option will selected 4 or 5 input text 3 must be filled. Ok?!

Thanks!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Mar 6 '08

re: Validate input if options combo selected


Expand|Select|Wrap|Line Numbers
  1. function validate () {
  2. valor = document.form.select.value;
  3.  
  4. if (valor == "1" || valor == "2" || valor == "3") {
  5.   if (input1 == "") {
  6.     alert("Input 1 must be filled.");
  7.     document.form.input1.focus();
  8.     return false;
  9.   } else if (input2 == "") {
  10.     alert("Input 2 must be filled.");
  11.     document.form.input2.focus();
  12.         return false;
  13.   }
  14. } else {
  15.     if (input3 == "") {
  16.     alert("Input 3 must be filled.");
  17.     document.form.input3.focus();
  18.         return false;
  19.     }
  20. }
  21.     return true;
  22. }
  23.  
mariodavi's Avatar
Newbie
 
Join Date: Feb 2008
Location: Jampa - Brasil
Posts: 8
#5: Mar 7 '08

re: Validate input if options combo selected


Dont works, cause always loop to first condition - Input 1 must be fille. =/

Quote:

Originally Posted by acoder

Expand|Select|Wrap|Line Numbers
  1. function validate () {
  2. valor = document.form.select.value;
  3.  
  4. if (valor == "1" || valor == "2" || valor == "3") {
  5.   if (input1 == "") {
  6.     alert("Input 1 must be filled.");
  7.     document.form.input1.focus();
  8.     return false;
  9.   } else if (input2 == "") {
  10.     alert("Input 2 must be filled.");
  11.     document.form.input2.focus();
  12.         return false;
  13.   }
  14. } else {
  15.     if (input3 == "") {
  16.     alert("Input 3 must be filled.");
  17.     document.form.input3.focus();
  18.         return false;
  19.     }
  20. }
  21.     return true;
  22. }
  23.  

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Mar 7 '08

re: Validate input if options combo selected


Quote:

Originally Posted by mariodavi

Dont works, cause always loop to first condition - Input 1 must be fille. =/

Have you not set input1, input2, etc. to document.form.input1.value, etc.?

Post the rest of the code especially how the function is called.
mariodavi's Avatar
Newbie
 
Join Date: Feb 2008
Location: Jampa - Brasil
Posts: 8
#7: Mar 7 '08

re: Validate input if options combo selected


And now, nothing works in the page =/

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function select_validate(formulario,campo,rotulo){
if(document[formulario][campo].selectedIndex == "0" ){
document[formulario][campo].focus();
alert('Especifique '+rotulo+'!');
return false;
}
return true;
}
</script>
<script type="text/javascript">

function validate () {
// Option must be selected
if (!select_validate('form','comboselect','Option')) {
return false;
}
valor = document.form.comboselect.value;
if (valor === "1" || valor === "2" || valor === "3") {
if (input1 == "") {
alert("Input 1 must be filled.");
document.form.input1.focus();
return false;
} else if (input2 == "") {
alert("Input 2 must be filled.");
document.form.input2.focus();
return false;
} else {
if (input3 == "") {
alert("Input 3 must be filled.");
document.form.input3.focus();
return false;
}
}
return true;
}
</script>
</head>

<body>
<form action="test_validate.html" method="post" name="form" onsubmit="return validate()">

<select name="comboselect">
<option value="0" selected>-------------</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select><br />

<label>Input 1</label><input name="input1" size="40" type="text" /><br />
<label>Input 2</label><input name="input2" size="40" type="text" /><br />
<label>Input 3</label><input name="input3" size="40" type="text" /><br />
<input type="submit" value="Send" title="Send" />
</form>
</body>
</html>
[/HTML]
Quote:

Originally Posted by acoder

Have you not set input1, input2, etc. to document.form.input1.value, etc.?

Post the rest of the code especially how the function is called.

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Mar 7 '08

re: Validate input if options combo selected


Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future. Thanks!

Moderator.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Mar 7 '08

re: Validate input if options combo selected


Quote:

Originally Posted by mariodavi

And now, nothing works in the page =/

That's because you've gone back to your old code! If you check the error console, you will see that you have a missing bracket. The inputs also need to be declared:
Expand|Select|Wrap|Line Numbers
  1. function validate () {
  2.     // Option must be selected
  3.     if (!select_validate('form','comboselect','Option')) { 
  4.         return false;
  5.     }
  6.     valor = document.form.comboselect.value;
  7.     input1 = document.form.input1.value;
  8.     input2 = document.form.input2.value;
  9.     input3 = document.form.input3.value;
  10.     if (valor === "1" || valor === "2" || valor === "3") {
  11.         if (input1 == "") {
  12.             alert("Input 1 must be filled.");
  13.             document.form.input1.focus();
  14.             return false;
  15.         } else if (input2 == "") {
  16.             alert("Input 2 must be filled.");
  17.             document.form.input2.focus();
  18.             return false;
  19.         }
  20.     } else {
  21.         if (input3 == "") {
  22.             alert("Input 3 must be filled.");
  23.             document.form.input3.focus();
  24.             return false;
  25.         }
  26.     }
  27.     return true;
  28. }
mariodavi's Avatar
Newbie
 
Join Date: Feb 2008
Location: Jampa - Brasil
Posts: 8
#10: Mar 7 '08

re: Validate input if options combo selected


Thanks acoder =]

Sorry for the code unformated!

Quote:

Originally Posted by acoder

That's because you've gone back to your old code! If you check the error console, you will see that you have a missing bracket. The inputs also need to be declared:

Expand|Select|Wrap|Line Numbers
  1. function validate () {
  2.     // Option must be selected
  3.     if (!select_validate('form','comboselect','Option')) { 
  4.         return false;
  5.     }
  6.     valor = document.form.comboselect.value;
  7.     input1 = document.form.input1.value;
  8.     input2 = document.form.input2.value;
  9.     input3 = document.form.input3.value;
  10.     if (valor === "1" || valor === "2" || valor === "3") {
  11.         if (input1 == "") {
  12.             alert("Input 1 must be filled.");
  13.             document.form.input1.focus();
  14.             return false;
  15.         } else if (input2 == "") {
  16.             alert("Input 2 must be filled.");
  17.             document.form.input2.focus();
  18.             return false;
  19.         }
  20.     } else {
  21.         if (input3 == "") {
  22.             alert("Input 3 must be filled.");
  23.             document.form.input3.focus();
  24.             return false;
  25.         }
  26.     }
  27.     return true;
  28. }

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Mar 7 '08

re: Validate input if options combo selected


No problem. Glad to hear that it now works.
Reply